哪些方法不能实施Spring AOP事务

由于Spring事务管理是基于接口代理或动态字节码技术,通过AOP实施事务增强的。虽然Spring还支持AspectJ LTW在类加载期实施增强,但这种方法很少使用,所以我们不予关注。 
    对于基于接口动态代理的AOP事务增强来说,由于接口的方法都必然是public的,这就要求实现类的实现方法也必须是public的(不能是protected、private等),同时不能使用static的修饰符。所以,可以实施接口动态代理的方法只能是使用“public”或“public final”修饰符的方法,其他方法不可能被动态代理,相应的也就不能实施AOP增强,换句话说,即不能进行Spring事务增强了。 
    基于CGLib字节码动态代理的方案是通过扩展被增强类,动态创建其子类的方式进行AOP增强植入的。由于使用final、static、private修饰符的方法都不能被子类覆盖,相应的,这些方法将无法实施AOP增强。所以方法签名必须特别注意这些修饰符的使用,以免使方法不小心成为事务管理的漏网之鱼。 

事务增强遗漏实例  

   本节中,我们通过具体的实例说明基于CGLib字节码动态代理无法享受Spring AOP事务增强的特殊方法。 
  
Java代码   收藏代码
  1. package com.baobaotao.special;  
  2. import org.springframework.stereotype.Service;  
  3. @Service("userService")  
  4. public class UserService {  
  5.   
  6.     //① private方法因访问权限的限制,无法被子类覆盖  
  7.     private void method1() {  
  8.         System.out.println("method1");  
  9.     }  
  10.   
  11.     //② final方法无法被子类覆盖  
  12.     public final void method2() {  
  13.         System.out.println("method2");  
  14.     }  
  15.   
  16.     //③ static是类级别的方法,无法被子类覆盖  
  17.     public static void method3() {  
  18.         System.out.println("method3");  
  19.     }  
  20.   
  21.     //④ public方法可以被子类覆盖,因此可以被动态字节码增强  
  22.     public void method4() {  
  23.         System.out.println("method4");  
  24.     }   
  25. }  


   Spring通过CGLib动态代理技术对UserService Bean实施AOP事务增强的关键配置,具体如下所示: 

Xml代码   收藏代码
  1. …  
  2.     <aop:config proxy-target-class="true"><!-- ①显式使用CGLib动态代理 -->  
  3.   
  4.         <!-- ②希望对UserService所有方法实施事务增强 -->  
  5.         <aop:pointcut id="serviceJdbcMethod"  
  6.                       expression="execution(* com.baobaotao.special.UserService.*(..))"/>  
  7.         <aop:advisor pointcut-ref="serviceJdbcMethod" advice-ref="jdbcAdvice" order="0"/>  
  8.     </aop:config>  
  9.     <tx:advice id="jdbcAdvice" transaction-manager="jdbcManager">  
  10.         <tx:attributes>  
  11.             <tx:method name="*"/>  
  12.         </tx:attributes>  
  13.     </tx:advice>  
  14. …  


   在①处,我们通过proxy-target-class="true"显式使用CGLib动态代理技术,在②处通过AspjectJ切点表达式表达UserService所有的方法,希望对UserService所有方法都实施Spring AOP事务增强。 
   在UserService添加一个可执行的方法,如下所示: 
Java代码   收藏代码
  1. package com.baobaotao.special;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. @Service("userService")  
  7. public class UserService {  
  8.     …  
  9.     public static void main(String[] args) {  
  10.         ApplicationContext ctx =   
  11.              new ClassPathXmlApplicationContext("user/special/applicationContext.xml");  
  12.         UserService service = (UserService) ctx.getBean("userService");  
  13.   
  14.         System.out.println("before method1");  
  15.         service.method1();  
  16.         System.out.println("after method1");  
  17.   
  18.         System.out.println("before method2");  
  19.         service.method2();  
  20.         System.out.println("after method2");  
  21.   
  22.         System.out.println("before method3");  
  23.         service.method3();  
  24.         System.out.println("after method3");  
  25.   
  26.         System.out.println("before method4");  
  27.         service.method4();  
  28.         System.out.println("after method4");  
  29.   
  30.     }  
  31. }  


   在运行UserService之前,将Log4J日志级别设置为DEBUG,运行以上代码查看输出日志,如下所示: 
引用

①未启用事务 
before method1 
in method1 
after method1 

②未启用事务 
before method2 
in method2 
after method2 

③未启用事务 
before method3 
in method3 
after method3 

④启用事务 
before method4 
Creating new transaction with name [com.baobaotao.special.UserService.method4]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT 
… 
in method4 
Initiating transaction commit 
Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost, MySQL-AB JDBC Driver] 
Releasing JDBC Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost, MySQL-AB JDBC Driver] after transaction 
Returning JDBC Connection to DataSource 
after method4 

⑤未用事务 
before method5 
in method5 
after method5 

⑥启用事务 
before method6 
Creating new transaction with name [com.baobaotao.special.UserService.method6]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT 
… 
in method6 
Initiating transaction commit 
Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost, MySQL-AB JDBC Driver] 
… 
after method6 


  观察以上输出日志,很容易发现method1~method3及method5这4个方法都没有被实施Spring的事务增强,而method4和method6被实施了事务增强。这个结果验证了我们前面的论述。 
  我们通过下表描述哪些特殊方法将成为Spring AOP事务增强的漏网之鱼。 
序    号动态代理策略不能被事务增强的方法
1基于接口的动态代理除public外的其他所有的方法,此外public static也不能被增强
2基于CGLib的动态代理private、static、final的方法


   不过,需要特别指出的是,这些不能被Spring事务增强的特殊方法并非就不工作在事务环境下。只要它们被外层的事务方法调用了,由于Spring事务管理的传播级别,内部方法也可以工作在外部方法所启动的事务上下文中。我们说,这些方法不能被Spring进行AOP事务增强,是指这些方法不能启动事务,但是外层方法的事务上下文依旧可以顺利地传播到这些方法中。 
   这些不能被Spring事务增强的方法和可被Spring事务增强的方法唯一的区别在于“是否可以主动启动一个新事务”:前者不能而后者可以。对于事务传播行为来说,二者是完全相同的,前者也和后者一样不会造成数据连接的泄漏问题。换句话说,如果这些“特殊方法”被无事务上下文的方法调用,则它们就工作在无事务上下文中;反之,如果被具有事务上下文的方法调用,则它们就工作在事务上下文中。 
   对于private的方法,由于最终都会被public方法封装后再开放给外部调用,而public方法是可以被事务增强的,所以基本上没有什么问题。在实际开发中,最容易造成隐患的是基于CGLib的动态代理时的“public static”和“public final”这两种特殊方法。原因是它们本身是public的,因此可以直接被外部类(如Web层的Controller类)调用,只要调用者没有事务上下文,这些特殊方法也就以无事务的方式运作。 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值