有没有办法在Spring AOP中获取被拦截方法的调用者(MVC更具体)?我有两个方法说“callerM1()”和“callerM2()”,它们调用了一个被拦截的方法“method()”。然后我有一个这样的方面:
@Before("execution(* *.method(..)) && args(arg1)")
public Object doSomething(...){
//find out which one and do something
}
Run Code Online (Sandbox Code Playgroud)
如何仅使用 Spring AOP 功能来了解“callerM1()”或“callerM2()”中的哪一个调用了“method()”?在这里我也可以使用 around 建议,但我想这是一个不同的问题。我检查了各种可能性,包括 EnclosureStaticPart 和更改切入点定义但没有成功。
一个快速的解决方案是使用 StackTraceElement,但我认为这不是一个好的解决方案。
这里提供了一个解决方案,需要完整的aspectj而不仅仅是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();
}
}
Run Code Online (Sandbox Code Playgroud)