-->

Accessing Annotation-value in advice

2020-07-24 05:19发布

问题:

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?

回答1:

@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 { ... }