簡體   English   中英

獲取Spring AOP中截獲方法的調用者

[英]Getting the Caller of the intercepted method in Spring AOP

有沒有辦法在Spring AOP中獲取截獲方法的調用者(MVC更具體)? 我有兩個方法說“callerM1()”和“callerM2()”調用截獲的方法“method()”。 然后我有這樣一個方面:

@Before("execution(* *.method(..)) && args(arg1)")
public Object doSomething(...){
    //find out which one and do something
} 

如何通過僅使用Spring AOP功能來學習“callerM1()”或“callerM2()”中的哪一個調用“method()”? 在這里我也可以使用周圍的建議,但我想這是一個不同的問題。 我檢查了各種可能性,包括EnclosingStaticPart和更改切入點定義但沒有成功。

一個快速的解決方案是使用StackTraceElement但我認為不是一個好的。

這里提供一個解決方案,需要完整的方面而不僅僅是彈簧。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM