Skip Object Between Two Around Functions - AOP

I perform an audit for my level of "Controller", "Service" and "Tao". I have three Around aspect functions for Controller, Service and Dao respectively. I use a custom annotation, which, if present in the Controller method, calls the Around aspect function. Inside the annotation, I set the property that I want to pass from the Controller Around function to the Service function around the Aspect class.

public @interface Audit{
   String getType();
}

I set the value of this getType from the interface.

@Around("execution(* com.abc.controller..*.*(..)) && @annotation(audit)")
public Object controllerAround(ProceedingJoinPoint pjp, Audit audit){
  //read value from getType property of Audit annotation and pass it to service around function
}

@Around("execution(* com.abc.service..*.*(..))")
public Object serviceAround(ProceedingJoinPoint pjp){
  // receive the getType property from Audit annotation and execute business logic
}

How to transfer an object between two Around functions?

+4
source share
1 answer

singleton. , , , . percflow(pointcut), . , :

@Aspect("percflow(controllerPointcut())")
public class Aspect39653654 {

    private Audit currentAuditValue;

    @Pointcut("execution(* com.abc.controller..*.*(..))")
    private void controllerPointcut() {}

    @Around("controllerPointcut() && @annotation(audit)")
    public Object controllerAround(ProceedingJoinPoint pjp, Audit audit) throws Throwable {
        Audit previousAuditValue = this.currentAuditValue;
        this.currentAuditValue = audit;
        try {
            return pjp.proceed();
        } finally {
            this.currentAuditValue = previousAuditValue;
        }
    }

    @Around("execution(* com.abc.service..*.*(..))")
    public Object serviceAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("current audit value=" + currentAuditValue);
        return pjp.proceed();
    }

}
+4

Source: https://habr.com/ru/post/1655568/


All Articles