Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。

具体如下图:

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

第一种方式:每个Bean都有一个代理

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定义事务管理器(声明式的事务) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置DAO --><bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="userDao"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  <!-- 配置事务管理器 -->  <property name="transactionManager" ref="transactionManager" />     <property name="target" ref="userDaoTarget" />  <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /><!-- 配置事务属性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop></props>  </property>  </bean>
</beans>

第二种方式:所有Bean共享一个代理基类

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定义事务管理器(声明式的事务) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="transactionBase"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  lazy-init="true" abstract="true">  <!-- 配置事务管理器 -->  <property name="transactionManager" ref="transactionManager" />  <!-- 配置事务属性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop>  </props>  </property>  </bean>    <!-- 配置DAO --><bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="userDao" parent="transactionBase" >  <property name="target" ref="userDaoTarget" />   </bean>
</beans>

第三种方式:使用拦截器

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定义事务管理器(声明式的事务) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean> <bean id="transactionInterceptor"  class="org.springframework.transaction.interceptor.TransactionInterceptor">  <property name="transactionManager" ref="transactionManager" />  <!-- 配置事务属性 -->  <property name="transactionAttributes">  <props>  <prop key="*">PROPAGATION_REQUIRED</prop>  </props>  </property>  </bean><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  <property name="beanNames">  <list>  <value>*Dao</value></list>  </property>  <property name="interceptorNames">  <list>  <value>transactionInterceptor</value>  </list>  </property>  </bean>  <!-- 配置DAO --><bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl"><property name="sessionFactory" ref="sessionFactory" /></bean>
</beans>

第四种方式:使用tx标签配置的拦截器

<?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"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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><context:annotation-config /><context:component-scan base-package="com.bluesky" /><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定义事务管理器(声明式的事务) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED" /></tx:attributes></tx:advice><aop:config><aop:pointcut id="interceptorPointCuts"expression="execution(* com.bluesky.spring.dao.*.*(..))" /><aop:advisor advice-ref="txAdvice"pointcut-ref="interceptorPointCuts" />        </aop:config>
</beans>

第五种方式:全注解

<?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"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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><context:annotation-config /><context:component-scan base-package="com.bluesky" /><tx:annotation-driven transaction-manager="transactionManager"/><bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <property name="configLocation" value="classpath:hibernate.cfg.xml" />  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /></bean>  <!-- 定义事务管理器(声明式的事务) -->  <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean></beans>

此时在DAO上需加上@Transactional注解,如下:

package com.bluesky.spring.dao;import java.util.List;import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;import com.bluesky.spring.domain.User;@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {public List<User> listUsers() {return this.getSession().createQuery("from User").list();}}

转载于:https://www.cnblogs.com/gy19920604/p/5288613.html

Spring 事务配置5种方式相关推荐

  1. spring事务管理几种方式(转)

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  2. Spring事务配置的五种方式 说明

    Spring事务配置的五种方式  [转 http://blog.csdn.net/hjm4702192/article/details/17277669] Spring配置文件中关于事务配置总是由三个 ...

  3. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别、不可重复读与幻读的区别

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spr ...

  4. spring(三)之事务、事务管理器(TransactionManager)简介及实现事务的4种方式

    1.事务 一组业务ACID操作,要么全部成功,要么全部不成功. 事务特性:①原子性,针对整体而言(一个事务不可以被拆分):②一致性,针对数据而言(一个事务执行之前和执行之后必须处于一致性状态,一个事务 ...

  5. spring事务配置,声明式事务管理和基于@Transactional注解的使用

    事务管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的一致性. spring提供了几个关于事务处理的类: TransactionDefinition //事务属性定义 Transc ...

  6. 6、Spring事务配置上篇

    一.事务简介 1.概述 1.事务在逻辑上一组操作,要么都执行(成功),要么都不执行(失败),主要是针对数据库而言的,比如MySQL.Oracle等. 2.事务是数据库提供的特性,因此可以直接通过操作数 ...

  7. Spring框架(五):Spring事务简述(注解方式)

    一.Spring事务的概述 事务的作用是什么? 简单来说,就是在数据层保障一系列的数据库操作同成功同失败(提交和回滚) Spring事务作用︰在数据层或[业务层]保障一系列的数据库操作同成功同失败. ...

  8. springboot实战 获取spring上下文的4种方式

    实际开发中我们经常需要通过spring上下文获取一些配置信息,本文阐述springboot应用获取spring上下文的几种方式. 方式一:实现ApplicationContextAware接口 imp ...

  9. Spring集成Memcached三种方式(一)

    Spring集成Memcached三种方式(一) 转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Clien ...

最新文章

  1. 解决安卓系统写入SD卡权限问题
  2. flask-WTF和sqlalchemy结合使用并实现前端页面登录(综合使用)
  3. 27. 代码实例-spring声明式事务
  4. 【AI】人工智能深度学习入门路线
  5. 缓存设计--读写锁场景实现
  6. vscode安装本地服务器_VS Code安装和远程服务器连接配置
  7. 一文读懂vuex4源码,原来provide/inject就是妙用了原型链?
  8. Python----socket编程
  9. ABP架构学习系列二:ABP中配置的注册和初始化
  10. 使用 SpringBoot 写 RESTful风格 增删改查接口
  11. 计算机组装流程是什么,组装电脑的步骤
  12. spring-security 实现单点登录
  13. vim 快速删除一个英文单词
  14. ROS 学习踩坑笔记5-Intel D435相机不发布点云,在RVIZ中,不显示点云(wants topic /image/compressed to have datatype/md5sum)
  15. scratch躲避球
  16. 【Zotero高效知识管理】(4)Zotero的文献管理、阅读及笔记知识管理
  17. 【C语言笔记】【宏定义系列】 绝对值
  18. 大学生计算机适合用苹果笔记本吗,2019学生党笔记本推荐 苹果笔记本适合大学生吗...
  19. 基于 Mixup 数据增强的 LSTM-FCN 时间序列分类学习记录
  20. c语言贝塞尔函数J1,贝塞尔函数:_j0、_j1、_jn、_y0、_y1、_yn

热门文章

  1. html两个框架同时_两个框架的故事
  2. 分类决策树 回归决策树_决策树分类器背后的数学
  3. stm32 USB增加端点总结
  4. linux内核与用户空间的九种通信机制
  5. 欢迎使用CSDN-markdown编辑器test
  6. 387. First Unique Character in a String QuestionEditorial Solution
  7. python frame如何置顶_Python tkinter frame父窗口小部件排列列
  8. 怎么画单极交流放大电路波形图_珠海放大IC怎么样
  9. matlab数组存字符串,MATLAB字符串数组存储为CSV格式
  10. python 装饰器有哪些_Python装饰器有哪些常见用途?