【问题标题】:Spring aspect call super.method() or inter method call unable to execute aspect codeSpring切面调用super.method()或方法间调用无法执行切面代码
【发布时间】:2014-11-07 07:03:16
【问题描述】:

我正在使用 spring aspect -around 建议; 对于 super.method() 或类间方法调用,我无法调用我的方面代码; 这是我的代码示例。

//My aspect Class
@Aspect
public class MyAspect {

    @Around("execution(* in.test.project.service.myservice.create(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    //  . 
        //my logic
    //  .
}

public interface Myservice{
    void create(Myclass entity);
    void createObject(Myclass entity);
}

class myserviceImpl implements Myservice{
    public void create(Myclass entity) {
        // TODO Auto-generated method stub
    }
    void createObject(Myclass entity){
        create(entity);
    }
}

    class Mycontroller {
        void test(Myclass entity) {
            Myservice myservice = new myserviceImplimplements();
            // calls MyAspect code
            myservice.create(entity);

            // MyAspect not called
            myservice.createObject(entity);
        }

    }

【问题讨论】:

    标签: java spring-aop


    【解决方案1】:

    这是使用 Spring AOP 的限制,而您的代码不知道它正在使用 Spring AOP(您可以查看 Spring 的文档,了解如何执行此操作以及为什么不应该 on their "Understanding AOP proxies" page)。 Spring AOP 是一个基于代理的系统,它区分了代理对象本身(绑定到 this)和代理背后的目标对象(绑定到 target),因此您可以应用的切入点类型也受到限制。

    由于调用首先通过代理然后在对象上进行,因此在对象内部对该对象进行的调用(如您上面调用内部方法的调用)将在 对象本身上调用 而不是代理。通过代理的调用可以委托给相关的拦截器,而由对象本身进行的调用不代理,因此不能像这样被 Spring AOP 通知。我在上面提供的链接显示了公开 AopContext,但我强烈建议不要这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多