package com.bluesky;
public interface FooServiceIface {
public void insertA();
public void insertB();
}
package com.bluesky
KLE's answer is solid advice. Just to complete the picture, there is a workaround, but it breaks everything AOP stands for:
public void insertA() {
this.getJdbcTemplate().execute("insert student(name) values('stuA')");
// this works, but... gah!
((FooServiceIface) AopContext.currentProxy()).insertB();
int i=10/0;
}
Reference:
I understand your problem. There are some technically complex ways to get it work, but we don't usually consider they are worth it. I suggest another approach, simple and powerful, that actually improves your code.
Instead of having them on the same Spring bean, have them on two different Beans, B being injected into A.
I known I'm not answering your question, but think about the advantages :
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.