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

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

I have below annotation.

MyAnnotation.java

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

}
         


        
5条回答
  •  清歌不尽
    2020-12-12 15:52

    Find working code for Method Annotation and class level annotation using AspectJ/AOP

       @Around("execution(* com.first.test.controller..*(..)))")
        public Object profileAllMethods(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
        {
            MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
    
            java.lang.reflect.Method method = methodSignature.getMethod();
    
      Annotation []methodAnnotations =  method.getDeclaredAnnotations();
            System.out.println("==============="+methodAnnotations[0].toString());
    
            Annotation []classAnnotations = proceedingJoinPoint.getTarget().getClass().getAnnotations();
    
            System.out.println("===Class Annotation : "+classAnnotations[1].toString());
           Object result = proceedingJoinPoint.proceed();
            return result;
        }
    

提交回复
热议问题