fturkmen
fturkmen

Reputation: 334

Getting the Caller of the intercepted method in Spring AOP

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.

Upvotes: 6

Views: 3992

Answers (1)

tkruse
tkruse

Reputation: 10685

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();
    }
}

Upvotes: 1

Related Questions