spring bean中this内部调用事物不起作用

来源:互联网 发布:尚观mysql视频教程 编辑:程序博客网 时间:2024/04/28 13:06

事例dome

interface IA{      void a();      void b();}class A implement IA{      @Transactional      public void a(){}      @Transactional      public void b(){    this.a();      }}class Test{      @Autowired      private A a;      public void test(){            a.b();      }}

这段代码中A中的方法b的事务不会起作用,研究了一下,以下是一些个人的简单理解,如有不正确还请指正。
spring中的bean是通过aop代理过的bean,以动态代理的方式实现了事务的环绕通知。
我们可以通过Proxy类来分析类A中的this所指向的类。
以下是一个Proxy动态代理类动态生成的代理类字节码文件反编译的类。

import java.lang.reflect.*;   public final class ProxyA extends Proxy implements IA{       private static Method m1;       private static Method m0;       private static Method m3;       private static Method m2;       public ProxySubject(InvocationHandler invocationhandler)       {           super(invocationhandler);       }       public final boolean equals(Object obj)       {           try          {               return ((Boolean)super.h.invoke(this, m1, new Object[] {                   obj               })).booleanValue();           }           catch(Error _ex) { }           catch(Throwable throwable)           {               throw new UndeclaredThrowableException(throwable);           }       }       public final int hashCode()       {           try          {               return ((Integer)super.h.invoke(this, m0, null)).intValue();           }           catch(Error _ex) { }           catch(Throwable throwable)           {               throw new UndeclaredThrowableException(throwable);           }       }   > public final void a()   >     {   >         try  >         {   >             super.h.invoke(this, m3, null);   >             return;   >         }   >         catch(Error _ex) { }   >         catch(Throwable throwable)   >         {   >             throw new UndeclaredThrowableException(throwable);   >         }   >     }    public final String toString()       {           try          {               return (String)super.h.invoke(this, m2, null);           }           catch(Error _ex) { }           catch(Throwable throwable)           {               throw new UndeclaredThrowableException(throwable);           }       }       static        {           try          {               m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {                   Class.forName("java.lang.Object")               });               m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);               m3 = Class.forName("Subject").getMethod("b", new Class[0]);               m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);           }           catch(NoSuchMethodException nosuchmethodexception)           {               throw new NoSuchMethodError(nosuchmethodexception.getMessage());           }           catch(ClassNotFoundException classnotfoundexception)           {               throw new NoClassDefFoundError(classnotfoundexception.getMessage());           }       }   }  

看这里>引用部分
当我们通过代理生成的对象调用目标方法时,调用的是InvocationHandler.invoke(Object proxy, Method method, Object[] args) throws Throwable方法
我们再看下InvocationHandler中是如何调用到目标对象的目标方法的,通过反射方法method.invoke( a, args)来调用目标对象的目标方法。
所以,目标方法中的this对象为被代理对象本身,并不是proxy代理对象
这也说明了,在bean内部方法通过this相互调用时,并不是通过aop生成的代理类在调用,自然不会被spring自动加上事务机制。

原创粉丝点击