通过反射获取类上的注解


@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());
        }
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM