Search code examples
springaopspring-aop

Spring AOP: Getting parameters of the pointcut annotation


Consider I have defined the following aspect:

@Aspect
public class SampleAspect {

    @Around(value="@annotation(sample.SampleAnnotation)")
    public Object display(ProceedingJoinPoint joinPoint) throws Throwable {
        // ...
    }
}

and the annotation

public @interface SampleAnnotation {
    String value() default "defaultValue";
}

Is there a way to read the value parameter of the annotation SampleAnnotation in the display method if my aspect?

Thanks for your help, erik


Solution

  • Change the advice signature to

    @Around(value="@annotation(sampleAnnotation)")
    public Object display(ProceedingJoinPoint joinPoint, SampleAnnotation sampleAnnotation ) throws Throwable {
        // ...
    }
    

    and you will have access to the value in the annotation.

    See docs for more info.