Java知识点总结(注解-解析注解)

Java知识点总结(注解-解析注解)

@(Java知识点总结)[Java, 注解]

通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。

使用注解步骤:

  1. 定义注解
  2. 类中使用注解
  3. 解析注解

示例:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(value={ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
  String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation {
  String columnName();
  String type();
  int length();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnnotation {
  String value();
}
@TableAnnotation("tb_student")
public class Student {
  @FieldAnnotation(columnName="id",type="int",length=10)
  private int id;
  @FieldAnnotation(columnName="sname",type="varchar",length=10)
  private String name;
  @FieldAnnotation(columnName="age",type="int",length=3)
  private int age;
  
  @MethodAnnotation("get方法上的注解")
  public int getId() {
   return id;
  }
  @MethodAnnotation("set方法上的注解")
  public void setId(int id) {
   this.id = id;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public int getAge() {
   return age;
  }
  public void setAge(int age) {
   this.age = age;
  }
  
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
 
public class Demo3 {
 
  // 使用反射获取注解,生成类对应的数据库表
  private static void test1(Class clazz) {
 
    // 获得类所有的注解
    Annotation[] annotations = clazz.getAnnotations();
    for (Annotation a : annotations) {
     System.out.println(a);
    }
 
    // 通过注解的名字获取注解
    TableAnnotation t = (TableAnnotation) clazz.getAnnotation(TableAnnotation.class);
    System.out.println(t.value());
 
    // 获得属性上的注解
    try {
     Field f = clazz.getDeclaredField("age");
     FieldAnnotation fa = f.getAnnotation(FieldAnnotation.class);
 
     System.out.println("字段名称:" + fa.columnName() + ",字段类型:" + fa.type() + ",字段长度:" + fa.length());
 
    } catch (NoSuchFieldException | SecurityException e) {
     e.printStackTrace();
    }
 
    // 根据获得的表名、字段信息,拼出DDL语句,然后使用JDBC执行这个SQL语句,在数据库中生成相关的表
 
  }
 
  //获取方法上的注解
  private static void test2(Class clazz) {
    // 获取所有方法上的注解
    Method[] ms = clazz.getMethods();
    for (Method m : ms) {
     boolean isExist = m.isAnnotationPresent(MethodAnnotation.class);
     if (isExist) {
       MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class);
       System.out.println(ma);
     }
    }
 
    // 获取指定方法上的注解
    try {
     Method method = clazz.getMethod("getId", null);
     Annotation[] as = method.getAnnotations();
     for (Annotation a : as) {
       if (a instanceof MethodAnnotation) {
         MethodAnnotation ma = (MethodAnnotation) a;
         System.out.println(ma);
       }
     }
 
    } catch (NoSuchMethodException | SecurityException e) {
     e.printStackTrace();
    }
  }
 
  public static void main(String[] args) {
    Class clazz = null;
    try {
     clazz = Class.forName("com.gs.Student");
    } catch (ClassNotFoundException e) {
     e.printStackTrace();
    }
    test1(clazz);
 
    test2(clazz);
 
  }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值