手动编码的方式实现事务的管理代码量大,代码有侵入性。
怎么解决这些问题呢?其实,我们可以考虑一个问题:事务管理其实就是在操作之前把connection打开,操作完毕之后关闭connection,并确保操作的事务性。是不是类似于增强了原来的数据库操作呢?
所以,声明式事务就是基于功能的增强。原始方式的声明式事务管理,是基于一个类:TransactionProxyFactoryBean(它可以生成一个基于事务的对象代理)
预备工作:新建一个项目,详见Spring学习-30项目环境搭建,另外,还需要引入aop包和aop联盟的包
接下来,我们就来介绍一下声明式事务管理(原始方式)的具体步骤:
1、配置applicationContext.xml:配置事务管理器和代理对象
<?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:context="http://www.springframework.org/schema/context"
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">
<!-- 配置属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义JDBC的模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 业务层类 -->
<bean id="accountService" class="com.js.demo2.AccountServiceImpl">
<!-- 在业务层注入Dao -->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 持久层类 -->
<bean id="accountDao" class="com.js.demo2.AccountDaoImpl">
<!-- 注入连接池对象,通过连接池对象去创建JDBC模板 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置生成代理对象 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 目标对象 -->
<property name="target" ref="accountService"></property>
<!-- 注入事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 设置事务的一些属性 -->
<property name="transactionAttributes">
<props>
<prop key="transfer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
【补充】:prop参数配置的格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
-顺序:传播行为、隔离级别、事务是否只读、发生哪些异常可以回滚事务(所有的异常都回滚)、发生了哪些异常不回滚
2、编写测试类
【注 】注入的时候,不能注入原来的service对象,而要注入代理对象!
package com.js.demo2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Test1 {
@Autowired
@Qualifier("accountServiceProxy")
private AccountService accountService;
@Test
public void demo(){
//完成转账
accountService.transfer("aaa", "bbb", 100d);
}
}
3、运行测试,转账成功。
关于发生异常时,事务回滚的测试此处不再赘述,跟上一讲同样(加个除零异常)测试即可。
缺点:就是需要为每一个管理事务的类生成代理,需要为每个类都进行配置。