I have below annotation.
MyAnnotation.java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
SomeAspect.java
public class SomeAspect{
@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
public Object procede(ProceedingJoinPoint call) throws Throwable {
//Some logic
}
}
SomeOther.java
public class SomeOther{
@MyAnnotation("ABC")
public String someMethod(String name){
}
}
In above class am passing "ABC" with in @MyAnnotation. Now how can i access "ABC" value in procede method of SomeAspect.java class?
Thanks!
You can get the Signature from a ProceedingJoinPoint and in case of a method invocation just cast it to a MethodSignature.
But you should first add an annotation attribute. Your example code doesn't have one, e.g.
Then you can access it
EDIT
A
Class
is also anAnnotatedElement
, so you can get it the same way as from aMethod
. E.g. An annotation of the method's declaring class can be obtained usingActually I think can get the
value
in another way round instead of just from ProceedingJoinPoint, which will definitely require us to make use ofreflection
.Have a try as follows using annotation directly: add
com.mycompany.MyAnnotation yourAnnotation
in youradvice params
and@annotation(yourAnnotation)
in@Around
.com.mycompany.MyAnnotation
in advice params just work as that inyourAnnotation
can be valid variable name sinceMyAnnotation
in params already points out which annotation it should be. HereyourAnnotation
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.
Or
This works only if your annotation is available at runtime, which you have declared correctly.