您的位置:首页 > 编程语言 > Java开发

Spring JDBC-实施Spring AOP事务注意事项及案例分析

2017-09-26 13:23 411 查看
实施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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: