Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Caller of the intercepted method in Spring AOP

Tags:

java

aop

aspectj

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.

like image 650
fturkmen Avatar asked Oct 30 '22 00:10

fturkmen


1 Answers

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();
    }
}
like image 111
tkruse Avatar answered Nov 15 '22 04:11

tkruse