1

I'm using some aspect around advice methods. I have methods for controller, service and repository.

@Around("execution(* com.abc..controller..*(..)) && @annotation(audit)")
    public Object controllerAround(ProceedingJoinPoint proceedingJoinPoint, Audit audit) throws Throwable {
        //some code here
        return output;
    }

@Around("execution(* com.abc..service.*Impl.*(..))")
    public Object serviceAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //some code here
        return output;
    }

@Around("execution(* com.abc..persistence.*Impl.*(..))")
    public Object persistenceAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //some code here
        return output;
    }

I have a query that, I need to check in serviceAround method pointcut expression that, whether it's comes from the controllerAround method. I tried using some flags, but spring doesn't support aspectj's if() pointcut primitive.

Any workaround for this will be appreciated. :)

Abhishek Ramachandran
  • 1,160
  • 1
  • 13
  • 34

1 Answers1

2

What you actually need is cflow() or cflowbelow(), not if(). But those are also not supported by Spring AOP. So the remaining solution is to use the full power of AspectJ from within Spring via LTW (load-time weaving). How this is done is nicely documented.

The pointcut would look like this:

execution(* com.abc..service.*Impl.*(..)) &&
cflow(
    execution(* com.abc..controller..*(..)) &&
    @annotation(customAnnotation)
)

Or simpler, assuming you do not need the annotation in the advice method:

execution(* com.abc..service.*Impl.*(..)) &&
cflow(execution(* com.abc..controller..*(..)))

Attention: cflow() only works for control flows within one thread, not if e.g. *Impl*.(..) is executed in another thread than controller..*(..).

P.S.: Your sample code would probably not work because of a parameter name mismatch between customAnnotation and audit.

kriegaex
  • 63,017
  • 15
  • 111
  • 202