javascript
Spring JDBC-实施Spring AOP事务注意事项及案例分析
- 實施SpringAOP事務注意事項
- 基于接口動態代理的AOP事務增強
- 基于CGLib字節碼動態代理的AOP事務增強
- 示例
- 特別說明
- 示例源碼
實施SpringAOP事務注意事項
眾所周知,Spring事務管理是基于接口代理或動態字節碼技術,通過AOP實施事務增強的,雖然Spring也支持AspectJ LTW在類加載期實施增強,但這種方法很少使用,我們先暫且不予理會,我們重點關注基于接口代理和動態字節碼技術
基于接口動態代理的AOP事務增強
接口必須是public,這就要求實現類的實現方法必須是public(不能使protected、private等)
同時不能使用static修飾符
所以,可以實施接口動態代理的方法只能是使用public或者public final修飾的方法,其他方法不可能被動態代理,相應的也就不能實施AOP增強,換句話說,即不能進行Spring事務增強。
基于CGLib字節碼動態代理的AOP事務增強
- 方法不能使用final 、static、private修飾符
基于CGLib字節碼動態代理的方法是通過擴展被增強類,動態創建其子類的方式進行AOP增強織入的 。 由于final、static、private修飾符的方法都不能被子類覆蓋,相應的這些方法將無法實施AOP增強。 所以方法簽名必須特別注意這些修飾符的使用,以免這些方法不小心不能被AOP織入事務增強。
示例
配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 --><context:component-scan base-package="com.xgj.dao.transaction.notice" /><!-- 使用context命名空間,配置數據庫的properties文件 --><context:property-placeholder location="classpath:spring/jdbc.properties" /><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close" p:driverClassName="${jdbc.driverClassName}"p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /><!-- 配置Jdbc模板 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"p:dataSource-ref="dataSource" /><!--基于數據源的事務管理器,通過屬性引用數據源 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"p:dataSource-ref="dataSource"/><aop:config proxy-target-class="true"><!-- 切點 --><aop:pointcut id="serviceMethod" expression="execution(* com.xgj.dao.transaction.notice.*Service.*(..))" /> <!-- 切面 --><aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/></aop:config><!-- 事務增強 --><tx:advice id="txAdvice" transaction-manager="transactionManager" ><!-- 事務屬性定義 --><tx:attributes><tx:method name="*"/></tx:attributes></tx:advice> </beans>通過proxy-target-class=“true”顯式使用CGLib動態代理技術,然后通過AspectJ切點表達式匹配目標類AopTransTestService的所有方法。希望對AopTransTestService所有方法都實施Spring AOP事務增強。
驗證類
package com.xgj.dao.transaction.notice;import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Service;/*** * * @ClassName: AOPTransTestService* * @Description: 在配置文件中開啟proxy-target-class="true" 使用CGlib動態字節碼技術織入AOP事務增強* * @author: Mr.Yang* * @date: 2017年9月26日 上午2:01:33*/@Service public class AopTransTestService {/*** * * @Title: method1* * @Description: private方法因為修飾符訪問權限的控制,無法被子類覆蓋* * * @return: void*/private void method1() {System.out.println("method1 executed");}/*** * * @Title: method2* * @Description: final方法無法被子類覆蓋* * * @return: void*/private final void method2() {System.out.println("method2 executed");}/*** * * @Title: method3* * @Description: static方法是類級別的方法,無法被子類覆蓋* * * @return: void*/private static void method3() {System.out.println("method3 executed");}/*** * * @Title: method4* * @Description: public方法可以被子類覆蓋,因此可以被動態字節碼增強* * * @return: void*/public void method4() {System.out.println("method4 executed");}/*** * * @Title: method5* * @Description: final方法無法被子類覆蓋* * * @return: void*/public final void method5() {System.out.println("method5 executed");}/*** * * @Title: method6* * @Description: protected方法可以被子類覆蓋,因此可以被動態字節碼增強* * * @return: void*/protected void method6() {System.out.println("method6 executed");}/*** * * @Title: main* * @Description: 測試* * @param args* * @return: void*/public static void main(String[] args) {ClassPathXmlApplicationContext ctx = null;AopTransTestService aopTransTestService = null;// 啟動Spring 容器ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/dao/transaction/notice/conf_tx_notice.xml");aopTransTestService = ctx.getBean("aopTransTestService",AopTransTestService.class);System.out.println("initContext successfully");System.out.println("before method1");aopTransTestService.method1();System.out.println("after1 method1");System.out.println("before method2");aopTransTestService.method2();System.out.println("after1 method2");System.out.println("before method3");aopTransTestService.method3();System.out.println("after1 method3");System.out.println("before method4");aopTransTestService.method4();System.out.println("after1 method4");System.out.println("before method5");aopTransTestService.method5();System.out.println("after1 method5");System.out.println("before method6");aopTransTestService.method6();System.out.println("after1 method6");if (ctx != null) {ctx.close();}System.out.println("close context successfully");} }關鍵日志分析
測試前,我們將log4j的日志級別設置為DEBUG
initContext successfully before method1 method1 executed after1 method1 before method2 method2 executed after1 method2 before method3 method3 executed after1 method3 before method4 2017-09-26 02:15:13,242 DEBUG [main] (AbstractPlatformTransactionManager.java:367) - Creating new transaction with name [com.xgj.dao.transaction.notice.AopTransTestService.method4]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT 2017-09-26 02:15:16,392 DEBUG [main] (DataSourceTransactionManager.java:248) - Acquired Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] for JDBC transaction 2017-09-26 02:15:16,407 DEBUG [main] (DataSourceTransactionManager.java:265) - Switching JDBC Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] to manual commit method4 executed 2017-09-26 02:15:16,456 DEBUG [main] (AbstractPlatformTransactionManager.java:759) - Initiating transaction commit 2017-09-26 02:15:16,457 DEBUG [main] (DataSourceTransactionManager.java:310) - Committing JDBC transaction on Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] 2017-09-26 02:15:16,653 DEBUG [main] (DataSourceTransactionManager.java:368) - Releasing JDBC Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] after transaction 2017-09-26 02:15:16,654 DEBUG [main] (DataSourceUtils.java:327) - Returning JDBC Connection to DataSource after1 method4 before method5 method5 executed after1 method5 before method6 2017-09-26 02:15:16,655 DEBUG [main] (AbstractPlatformTransactionManager.java:367) - Creating new transaction with name [com.xgj.dao.transaction.notice.AopTransTestService.method6]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT 2017-09-26 02:15:16,656 DEBUG [main] (DataSourceTransactionManager.java:248) - Acquired Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] for JDBC transaction 2017-09-26 02:15:16,656 DEBUG [main] (DataSourceTransactionManager.java:265) - Switching JDBC Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] to manual commit method6 executed 2017-09-26 02:15:16,656 DEBUG [main] (AbstractPlatformTransactionManager.java:759) - Initiating transaction commit 2017-09-26 02:15:16,656 DEBUG [main] (DataSourceTransactionManager.java:310) - Committing JDBC transaction on Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] 2017-09-26 02:15:16,759 DEBUG [main] (DataSourceTransactionManager.java:368) - Releasing JDBC Connection [jdbc:oracle:thin:@172.25.246.11:1521:testbed, UserName=CC, Oracle JDBC driver] after transaction 2017-09-26 02:15:16,760 DEBUG [main] (DataSourceUtils.java:327) - Returning JDBC Connection to DataSource after1 method6 2017-09-26 02:15:16,760 INFO [main] (AbstractApplicationContext.java:984) - Closing org.springframework.context.support.ClassPathXmlApplicationContext@541187f9: startup date [Tue Sep 26 02:15:11 BOT 2017]; root of context hierarchy從輸出可以很明顯的看到, 只有method4和method6被實施了Spring事務增強,驗證了我們之前的說法。
特別說明
不能被 Spring AOP 事務增強的方法:
| 基于接口的動態代理 | 除 public 外的其它所有的方法,此外 public static 也不能被增強 |
| 基于 CGLib 的動態代理 | private、static、final 的方法 |
不過,需要特別指出的是,這些不能被 Spring 事務增強的特殊方法并非就不工作在事務環境下。只要它們被外層的事務方法調用了,由于 Spring 的事務管理的傳播特殊,內部方法也可以工作在外部方法所啟動的事務上下文中。
我們說,這些方法不能被 Spring 進行 AOP 事務增強,是指這些方法不能啟動事務,但是外層方法的事務上下文依就可以順利地傳播到這些方法中。
這些不能被 Spring 事務增強的方法和可被 Spring 事務增強的方法唯一的區別在 “是否可以主動啟動一個新事務”:前者不能而后者可以。
對于事務傳播行為來說,二者是完全相同的,前者也和后者一樣不會造成數據連接的泄漏問題。換句話說,如果這些“特殊方法”被無事務上下文的方法調用,則它們就工作在無事務上下文中;反之,如果被具有事務上下文的方法調用,則它們就工作在事務上下文中。
對于 private 的方法,由于最終都會被 public 方法封裝后再開放給外部調用,而 public 方法是可以被事務增強的,所以基本上沒有什么問題。在實際開發中,最容易造成隱患的是基于 CGLib 的動態代理時的“public static”和“public final”這兩種特殊方法。原因是它們本身是 public 的,所以可以直接被外部類(如 Web 層的 Controller 類)調用,只要調用者沒有事務上下文,這些特殊方法也就以無事務的方式運作。
示例源碼
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
總結
以上是生活随笔為你收集整理的Spring JDBC-实施Spring AOP事务注意事项及案例分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript-WebStorm中
- 下一篇: Spring JDBC-混合框架的事务管