One Service method invoke inner multiple method for Spring transaction

前端 未结 3 1236
情话喂你
情话喂你 2020-12-20 17:13
package com.bluesky;

public interface FooServiceIface {
    public  void insertA();
    public void insertB();
}

package com.bluesky         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-20 18:05

    KLE's advice about refactoring your code is right on the money, but as for why it's not working, Spring AOP uses JDK dynamic proxies by default to provide AOP. That means that when you inject your service into something, what's really getting injected is a Proxy instance that implements your service interface. When a method is invoked on this proxy, it runs the transaction code before delegating to your actual service instance. Once the control flow is inside your service, though, calling another method via this.foo() (even if the this is implicit) just invokes a method on that same instance: your service. It doesn't go back to the proxy, which is the only thing that knows about transactions. If you switched to build- or load-time bytecode weaving with AspectJ, then you could do this, and it would work as expected, since the transaction-invoking code would be woven directly into your service code instead of living in a separate object.

提交回复
热议问题