Search code examples
spring-bootannotationsaopspring-aopspring-annotations

How to get the value from Custom Annotation in my advice method in Spring AOP?


I have my custom annotation like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CutomAnnotation{
    String value() default "";
}

My aspect class looks like this:

@Aspect
@Component
public class MyCustomAspect{

@Around("@annotation(com.forceframework.web.handlers.monitoring.MeterRegTimer)")
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable{
    System.out.println("Timer started: "+joinPoint.getSignature());
    Object objToReturn=joinPoint.proceed();
    System.out.println("Timer ended: "+joinPoint.getSignature());
    return objToReturn;
    }
}

The place I use the annotation in a controller class:

@CustomAnnotation(value="timer")
@GetMapping(value="/test")
public ResponseEntity test() {}

I would like to know can I access the value passed from my CustomAnnotation in the around advice method aroundJoinPoint in MyCustomAspect class.


Solution

  •     MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        CutomAnnotation methodAnnotation = method.getDeclaredAnnotation(CutomAnnotation.class);