【问题标题】:Getting the Caller of the intercepted method in Spring AOPSpring AOP中获取被拦截方法的调用者
【发布时间】:2018-01-30 13:38:14
【问题描述】:

有没有办法在 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()”?在这里,我也可以使用 Around 建议,但我想这是一个不同的问题。我检查了各种可能性,包括 EnclosureStaticPart 和更改切入点定义,但均未成功。

使用 StackTraceElement 是一种快速的解决方案,但我认为这不是一个好的解决方案。

【问题讨论】:

  • 最后你是如何实现的??

标签: java aop aspectj


【解决方案1】:

here 提供了一个解决方案,需要完整的 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();
    }
}

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2014-05-07
    相关资源
    最近更新 更多