Nested method invoke in the same instance issue of Spring AOP

    Today, Spring AOP is widely used in transaction manage, but I found something that I had never noticed yet.
    The situation was :  I have a service class named KeywordExtendService , there are 2 methods in it named: "start" and "service". the service method will invoke the start method. but I config the pointcut like : execution(* com.waynewan.KeywordExtendService .start(..)) .  and the transaction propagation level is "REQUIRED",  so this means if there is an transaction it will use the exist one, if not it will create one. but the result is not as I guessed. when the start method invoked in the service method(no transaction configuration), there is no transaction start.     Then i investigated it in the internet, and found something. There are some limitation in spring's transaction management which is based on AOP . When we get a bean from spring ioc container, actually we get a proxy of that bean, the proxy hold a reference to the real instance of that bean. so if we invoke a method on that real instance, actually we operate on the proxy, the proxy will add some like before, after method around the real invocation. like below:

  1. class Proxy{
  2.     Service(){
  3.        before();
  4.        realInstance.service();
  5.        after();
  6.     }
  7. }

    so if  the start method is invoked in the service method, the object which will be operated on is the real instance(not the proxy one). but in the real instance no before, after method can be added. so no transaction logic can be added. 
    So the solution is : change the pointcut to : execution(* com.waynewan.KeywordExtendService .service(..)) .

    for the complete example, please refer to : http://blog.csdn.net/konglong_king/article/details/5425298  

Leave a Reply

Your email address will not be published. Required fields are marked *

You must enable javascript to see captcha here!