反射获取注解值
1.获取变量注解上的属性值
注解类
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface primaryKey {
String name() default "";
}
测试方法
@Test
public void test(){
Student student = new Student();
for (Field field : student.getClass().getFields()) {
field.setAccessible(true);
if (field.isAnnotationPresent(primaryKey.class)){
String property=field.getAnnotation(primaryKey.class)==null?"":field.getAnnotation(primaryKey.class).name();
System.out.println(property);
}
}
}
实体类
@Data
public class Student {
@primaryKey(name = "java")
public String name;
public String address;
public int status;
}