通过反射获取类上的注解

@AnnotationField(tableName = "tb_animal", flag = 0)//使用多个注解值,中间用,割开,如果不想使用默认值,可以在注解中为其重新赋值
@AnnotationTest1("tb_animal")
public class Animal {
    private String name;
    private String sex;
    private Integer age;
}

比如获取Animal这个类上的注解:

public static void main(String[] args) throws Exception{
        Class<?> animal = Class.forName("annotation.Animal");
        AnnotationField annotationField = animal.getAnnotation(AnnotationField.class);
        if (annotationField!=null){
            //annotationField : @annotation.AnnotationField(flag=0, tableName=tb_animal)
            System.out.println("annotationField : "+annotationField);
            //0  tb_animal  interface annotation.AnnotationField
            System.out.println(annotationField.flag()+"  "+annotationField.tableName()+"  "+annotationField.annotationType());
        }
    }
原文地址:https://www.cnblogs.com/huanghuanghui/p/10164884.html