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

后端 未结 5 1575
谎友^
谎友^ 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:13

    Actually I think we can get the value in another way round instead of just from ProceedingJoinPoint, which will definitely require us to make use of reflection.

    Have a try as follows using annotation directly: add com.mycompany.MyAnnotation yourAnnotation in your advice params and @annotation(yourAnnotation) in @Around.

    @Around("execution(public * *(..)) && @annotation(yourAnnotation)")
    public Object procede(ProceedingJoinPoint pjp, com.mycompany.MyAnnotation yourAnnotation) {
        ...
        yourAnnotation.value(); // get your annotation value directly;
        ...
    }
    

    com.mycompany.MyAnnotation in advice params just work as that in

    @Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
    

    yourAnnotation can be valid variable name since MyAnnotation in params already points out which annotation it should be. Here yourAnnotation is used to retrieve the annotation instance only.

    If you want to pass more params you can try args().

    For more details, do please check its official doc. For Annotation value, you can just search @Auditable.

提交回复
热议问题