Search code examples
springannotationsaop

Accessing Annotation-value in advice


In my application I have methods annotated like this:

@SomeAnnotation(key1="value1", key2 ="value2")
public void myMethod()

I have defined the following apsect to perform some action on executing of those methods:

@Aspect
public class MyAspect()
{
    @Around("@annotation(my.package.SomeAnnotation)")
    public Object doSomething(final ProceedingJoinPoint pjp) throws Throwable
    {
        ...
    }
}

Now I would like to use the annotation values ("value1" and "value2" in the above example) inside my advice. What is the way to access the annotation at this point?


Solution

  • @annotation can be used in binding form, as follows:

    @Around(value = "@annotation(a)", argNames = "a")
    public Object doSomething(final ProceedingJoinPoint pjp, SomeAnnotation a)
        throws Throwable { ... }