Is there a way to get the caller of the intercepted method in Spring AOP (with MVC to be more specific)? I have two methods say "callerM1()" and "callerM2()" that call an intercepted method "method()". Then I have an aspect like this:
@Before("execution(* *.method(..)) && args(arg1)")
public Object doSomething(...){
//find out which one and do something
}
How can I learn which of "callerM1()" or "callerM2()" has called "method()" by using only the Spring AOP functionality? Here I could use Around advice as well but I guess that is a different issue. I checked various possibilities including EnclosingStaticPart and changing the pointcut definition with no success.
A quick solution was using StackTraceElement but I believe is not a good one.
A solution is provided here, requiring full aspectj and not just spring-aop.
@Aspect
public class TestAspect {
@Around("call(void com.spring.ioc.Aggregated.*(..))")
public Object advice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("xxxxxxxxxxxx: TestAspect.advice(): aspect is called on " + joinPoint
+ ". This object: " + joinPoint.getThis());
return joinPoint.proceed();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With