栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Reflect - 利用反射获取方法上的注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Reflect - 利用反射获取方法上的注解

文章目录
    • 1. AnnotatedElement接口
    • 2. Method类实现了AnnotatedElement接口
    • 3. 获取方法上的注解

1. AnnotatedElement接口

AnnotatedElement接口表示目前正在此 JVM 中运行的程序的一个已注释元素,该接口允许反射性地读取注释。

该接口主要有如下几个实现类:

  • Class:类定义
  • Constructor:构造器定义
  • Field:类的成员变量定义
  • Method:类的方法定义
  • Package:类的包定义

调用AnnotatedElement对象的如下方法可以访问Annotation信息:

  • getAnnotation(ClassannotationClass):返回该程序元素上存在的指定类型的注释,如果该类型的注释不存在,则返回null。
  • Annotation[] getAnnotations():返回此元素上存在的所有注释。
  • boolean isAnnotationPresent(ClassannotationClass):判断该程序元素上是否存在指定类型的注释,如果存在则返回true,否则返回false。
  • Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。
2. Method类实现了AnnotatedElement接口

3. 获取方法上的注解

① 自定义注解

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,ElementType.TYPE})
public @interface MyAnnotation {

    public int age();

    // 为name变量指定初始值
    public String name() default "张三";

}

② 给Test类的show1方法上加3个注解: @ResponseBody、 @PostMapping、@MyAnnotation(name = “李四”,age=12)

@ControllerAdvice
@Controller
public class Test {

    @ResponseBody
    @PostMapping
    @MyAnnotation(name = "李四",age=12)
    public void show1(){
        System.out.println("111");
    }

    @ResponseBody
    @PostMapping
    @MyAnnotation(name = "李四",age=12)
    public void show2(){
        System.out.println("111");
    }
}

③ 获取show1()方法上的指定类型的注解

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {

        // 得到Class类对象
        Class clazz = Class.forName("com.example.redislock.annotation.Test");

        // 获取Test类的show1方法
        Method method = clazz.getMethod("show1");

        // 获取show1()方法上的MyAnnotation注解
        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
        int age = annotation.age();
        String name = annotation.name();
        System.out.println("name="+name+", age="+age);
    }
}


④ 判断方法上是否存在指定类型的注解,并获取指定类型的注解

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {

        // 得到Class类对象
        Class clazz = Class.forName("com.example.redislock.annotation.Test");

        // 获取Test类的所有方法
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            // 判断方法上是否存在MyAnnotation类型的注解
           if(method.isAnnotationPresent(MyAnnotation.class)){
               // 获取方法上的MyAnnotation类型的注解
               MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
               int age = annotation.age();
               String name = annotation.name();
               System.out.println("name="+name+", age="+age);
           }
        }

    }
}


⑤ 获取所有方法的所有注解

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {

        // 得到Class类对象
        Class clazz = Class.forName("com.example.redislock.annotation.Test");

        // 获取Test类的所有方法
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            // 获取方法上的所有注解
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations) {
                // 如果注解类型为MyAnnotation类型
                if(annotation instanceof MyAnnotation){
                    MyAnnotation myAnnotation = (MyAnnotation) annotation;
                    int age = myAnnotation.age();
                    String name = myAnnotation.name();
                    System.out.println("name="+name+", age="+age);
                }
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/848211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号