概述
大多数开发者选择声明式事务管理的功能,这种方式对代码的侵入性最小,可以让事务管理完全从业务代码中移除,非常符合非侵入式轻量容器的理念。
Spring的声明式事务管理是通过AOP实现的,通过事务的声明性信息,Spring负责将事务管理增强逻辑动态的织入到业务方法的相应连接点中。 这些逻辑包括获取线程绑定资源、开始事务、提交/回滚事务、进行异常转换和处理等工作。
基于aop/tx命名空间的配置
Spring2.0引入了AspectJ切面定义语言,这使得事务方法切面描述变得更加简单。
Spring在基于Schema的配置中添加了一个tx命名空间,在配置文件中以明确结构化的方式定义事务属性,大大提高了配置事务属性的便利性。
示例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
我们来看下关于事务管理的配置文件:
<?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"> <!-- 引入其他xml --> <import resource="classpath:com/xgj/dao/transaction/xmlTrans/conf_txaop_base.xml"/> <!-- 使用强大的切点表达式语言轻松定义目标方法 --> <aop:config proxy-target-class="true"> <!-- 通过aop定义事务增强切面 --> <aop:pointcut id="serviceMethod" expression="execution(* com.xgj.dao.transaction.xmlTrans.service.*Service.*(..))" /> <!-- 引用切面和事务增强 --> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/> </aop:config> <!-- 事务增强 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <!-- 事务属性定义 --> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="add*" rollback-for="Exception"/> <tx:method name="update*"/> </tx:attributes> </tx:advice> </beans>
在配置文件中引入了
<import resource="classpath:com/xgj/dao/transaction/xmlTrans/conf_txaop_base.xml"/>
在aop命名空间中,通过切点表达式语言,
expression="execution(* com.xgj.dao.transaction.xmlTrans.service.*Service.*(..))"
将com.xgj.dao.transaction.xmlTrans.service包以及所有以Service为后缀的类纳入了需要进行事务增强的范围,并配合 tx:advice的aop:advisor完成了事务切面的定义。
aop:advisor引用txAdvice增强是在tx命名空间上定义的。 首先事务增强一定需要一个事务管理器的支持,tx:advice通过 transaction-manager属性引用定义的事务管理器(默认查找名为transactionManager的事务管理器,所以如果名为transactionManager可以不指定)。
我们在ManagerService类中将addTeacher和addStudent中放在一个事务中
package com.xgj.dao.transaction.xmlTrans.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xgj.dao.transaction.xmlTrans.dao.StudentDao; import com.xgj.dao.transaction.xmlTrans.dao.TeacherDao; import com.xgj.dao.transaction.xmlTrans.domain.Student; import com.xgj.dao.transaction.xmlTrans.domain.Teacher; /** * * * @ClassName: ManagerService * * @Description: @Service标注的Service层 * * @author: Mr.Yang * * @date: 2017年9月22日 下午10:18:31 */ @Service public class ManagerService { private TeacherDao teacherDao; private StudentDao studentDao; /** * * * @Title: addTeacherAndStudent * * @Description: 配置文件中<tx:method name="add*" rollback-for="Exception"/> * 我们故意经写入student表时,让其异常,看下teacher是否回滚--OK 都没有写入,说明在一个事务中 * 如果有去掉<tx:method name="add*" * rollback-for="Exception"/>,经验证Teacher可成功增加,说明不在一个事务中 * * rollback-for="Exception"这个可不加,默认是回滚的 * @param teacher * @param student * * @return: void */ public void addTeacherAndStudent(Teacher teacher, Student student) { teacherDao.addTeacher(teacher); studentDao.addStudent(student); } @Autowired public void setTeacherDao(TeacherDao teacherDao) { this.teacherDao = teacherDao; } @Autowired public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } }
因为在配置文件中
<tx:method name="add*" rollback-for="Exception"/>
所以addTeacherAndStudent中的方法在一个事务中,我们将addStudent中故意写错,让其抛出异常,经验证Teacher也没有欧增加成功,说明配置生效,在一个事务中。
tx:method元素属性
可以使用冒号来定义表格的对齐方式,如下:
继承自runtimeexception或error的是非检查型异常(运行期异常)
继承自exception的是检查型异常,检查型异常则必须用try语句块进行处理或者把异常交给上级方法处理.
如果需要为不同的业务Bean配置不同的事务管理风格,则可以在aop:config中定义多套事务切面。
基于aop/tx配置的声明式事务管理是实际应用中最常使用的事务管理方式,它的表达能力最强且使用最为灵活。

