通過反射獲取類上的注解


@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