How to get a method's annotation value from a ProceedingJoinPoint?

后端 未结 5 1578
谎友^
谎友^ 2020-12-12 15:41

I have below annotation.

MyAnnotation.java

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}
         


        
5条回答
  •  眼角桃花
    2020-12-12 16:09

    This works as well - You can fetch annotation information using reflection on the class.

    Annotation anno = MyClass.class.getAnnotation(MyAnnotation.class);
    

    Or

    Annotation anno = MyClass.class.getDeclaredMethod("somethod").getAnnotation(MyAnnotation.class);
    

    This works only if your annotation is available at runtime, which you have declared correctly.

    @Retention(RetentionPolicy.RUNTIME)
    

提交回复
热议问题