Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP: Getting parameters of the pointcut annotation

Consider I have defined the following aspect:

@Aspect public class SampleAspect {      @Around(value="@annotation(sample.SampleAnnotation)")     public Object display(ProceedingJoinPoint joinPoint) throws Throwable {         // ...     } } 

and the annotation

public @interface SampleAnnotation {     String value() default "defaultValue"; } 

Is there a way to read the value parameter of the annotation SampleAnnotation in the display method if my aspect?

Thanks for your help, erik

like image 454
Erik Avatar asked Mar 12 '11 11:03

Erik


People also ask

What is pointcut in Spring AOP?

In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points, and the pointcut expression language is a way of describing pointcuts programmatically.

What methods does pointcut expression reference?

Pointcut is an expression language of spring AOP which is basically used to match the target methods to apply the advice. It has two parts ,one is the method signature comprising of method name and parameters. Other one is the pointcut expression which determines exactly which method we are applying the advice to.

Which of the following option will match the given pointcut?

Explanation: Union means the methods that either pointcut matches. Intersection means the methods that both pointcuts match. Union is usually more useful. Explanation: Using the static methods in the org.


2 Answers

Change the advice signature to

@Around(value="@annotation(sampleAnnotation)") public Object display(ProceedingJoinPoint joinPoint, SampleAnnotation sampleAnnotation ) throws Throwable {     // ... } 

and you will have access to the value in the annotation.

See docs for more info.

like image 185
David Rabinowitz Avatar answered Sep 21 '22 14:09

David Rabinowitz


Bellow I'll add a complete example of AOP implementation where I'll getting parameter from my Custom pointCut annotation, where my advice aim to calculate the time execution of a function:

1- Custom Annotation:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AnnotationLogExecutionTime {      public boolean isActivate() default false;  } 

2- Controller:

@AnnotationLogExecutionTime(isActivate = true) @PostMapping("/connection") public HttpEntity<String> createAuthenticationToken(HttpServletRequest request,                                                         @RequestBody AuthenticationRequest authenticationRequest) {...} 

3- Advice

@Component @Aspect public class LoggingExecutionTimeAdvice {      @Around("@annotation(annotationLogExecutionTime)")     public Object logExecutionTime(ProceedingJoinPoint joinPoint, AnnotationLogExecutionTime annotationLogExecutionTime) throws Throwable {          if(annotationLogExecutionTime.isActivate()){//Here I recover the value!!!!             long start = System.currentTimeMillis();             Object proceed = joinPoint.proceed();             long executionTime = System.currentTimeMillis() - start;             System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");             return proceed;         }         Object proceed = joinPoint.proceed();         return proceed;     } } 

Explanation:

Our advice (logExecutionTime) will be excuted around (joinPoint) the function that will be annotated with AnnotationLogExecutionTime (our custom annotation) so I want to active or not this the calculation of time execution so I'll get the value from the membre of our custom annotation (which you ask about ;) )

like image 26
BERGUIGA Mohamed Amine Avatar answered Sep 22 '22 14:09

BERGUIGA Mohamed Amine