Spring AOP: Getting parameters of the pointcut annotation

Erik picture Erik · Mar 12, 2011 · Viewed 19k times · Source

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

Answer

David Rabinowitz picture David Rabinowitz · Mar 14, 2011

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.