I have my custom annotation like this:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CutomAnnotation{
String value() default "";
}
My Aspect class looks like this:
@Aspect
@Component
public class MyCustomAspect{
@Around("@annotation(com.forceframework.web.handlers.monitoring.MeterRegTimer)")
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("Timer started: " joinPoint.getSignature());
Object objToReturn=joinPoint.proceed();
System.out.println("Timer ended: " joinPoint.getSignature());
return objToReturn;
}
}
The place I use the annotation in a controller class:
.
.
.
@CustomAnnotation(value="timer")
@GetMapping(value="/test")
public ResponseEntity test(){
}
.
.
.
I would like to know can I access the 'value' passed from my 'CustomAnnotation' in the Around advice method-'aroundJoinPoint' in 'MyCustomAspect'class.
Blockquote
CodePudding user response:
Your advice should be declared as shown below:
@Around("@annotation(customAnnotationArgumentName)")
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotationArgumentName) throws Throwable {
// ...
}
See documentation for more info.
CodePudding user response:
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
CutomAnnotation methodAnnotation = method.getDeclaredAnnotation(CutomAnnotation.class);