How to get a method's annotation value from a ProceedingJoinPoint?
You can get the Signature from a ProceedingJoinPoint and in case of a method invocation just cast it to a MethodSignature.
@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
public Object procede(ProceedingJoinPoint call) throws Throwable {
MethodSignature signature = (MethodSignature) call.getSignature();
Method method = signature.getMethod();
MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
}
But you should first add an annotation attribute. Your example code doesn't have one, e.g.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
Then you can access it
MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
String value = myAnnotation.value();
EDIT
How to get value if I have @MyAnnotation("ABC") at class level ?
A Class
is also an AnnotatedElement
, so you can get it the same way as from a Method
. E.g. An annotation of the method's declaring class can be obtained using
Method method = ...;
Class<?> declaringClass = method.getDeclaringClass();
MyAnnotation myAnnotation = declaringClass.getAnnotation(MyAnnotation.class)
Since you are using spring you might also want to use spring's AnnotationUtils.findAnnotation(..)
. It searches for an annotation as spring does. E.g. also looking at superclass and interface methods, etc.
MyAnnotation foundAnnotation = AnnotationUtils.findAnnotation(method, MyAnnotation.class);
EDIT
You might also be interessted in the capabilities of spring's MergedAnnotations
which was introduced in 5.2.
Actually I think we can get the value
in another way round instead of just from ProceedingJoinPoint, which will definitely require us to make use of reflection
.
Have a try as follows using annotation directly: add com.mycompany.MyAnnotation yourAnnotation
in your advice params
and @annotation(yourAnnotation)
in @Around
.
@Around("execution(public * *(..)) && @annotation(yourAnnotation)")
public Object procede(ProceedingJoinPoint pjp, com.mycompany.MyAnnotation yourAnnotation) {
...
yourAnnotation.value(); // get your annotation value directly;
...
}
com.mycompany.MyAnnotation
in advice params just work as that in
@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
yourAnnotation
can be valid variable name since MyAnnotation
in params already points out which annotation it should be. Here yourAnnotation
is used to retrieve the annotation instance only.
If you want to pass more params you can try args()
.
For more details, do please check its official doc. For Annotation value, you can just search @Auditable
.
This works as well - You can fetch annotation information using reflection on the class.
Annotation anno = MyClass.class.getAnnotation(MyAnnotation.class);
Or
Annotation anno = MyClass.class.getDeclaredMethod("somethod").getAnnotation(MyAnnotation.class);
This works only if your annotation is available at runtime, which you have declared correctly.
@Retention(RetentionPolicy.RUNTIME)