全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Spring事务Transaction配置的五种注入方式详解

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

总结如下:

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.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd 
 http://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.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd 
 http://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.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd 
 http://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.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
 http://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.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://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();
 }  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# spring  # 注入事务  # 事务配置方式  # 注解事务配置  # mybatis事务管理配置与@Transactional注解使用详解  # Spring配置中transactionAttributes的使用方法介绍  # Spring中配置Transaction与不配置的区别及说明  # 管理器  # 五种  # 配置文件  # 都有  # 拦截器  # 在此  # 是由  # 要把  # 这部  # 比较好  # 一直没有  # 哪种  # 两部分  # 第二种  # 前段时间  # 第一种  # 组成部分  # 这三  # 大家多多  # 如下图 


相关文章: Android自定义控件实现温度旋转按钮效果  建站之星安装路径如何正确选择及配置?  如何快速查询网址的建站时间与历史轨迹?  建站之星如何通过成品分离优化网站效率?  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  如何在宝塔面板中创建新站点?  h5网站制作工具有哪些,h5页面制作工具有哪些?  定制建站平台哪家好?企业官网搭建与快速建站方案推荐  ,如何利用word制作宣传手册?  ,怎么在广州志愿者网站注册?  广德云建站网站建设方案与建站流程优化指南  一键网站制作软件,义乌购一件代发流程?  c++ stringstream用法详解_c++字符串与数字转换利器  手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?  网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?  h5在线制作网站电脑版下载,h5网页制作软件?  淘宝制作网站有哪些,淘宝网官网主页?  如何在阿里云购买域名并搭建网站?  建站主机核心功能解析:服务器选择与网站搭建流程指南  小程序网站制作需要准备什么资料,如何制作小程序?  建站之星导航如何优化提升用户体验?  建站之星如何快速解决建站难题?  电脑免费海报制作网站推荐,招聘海报哪个网站多?  建站主机如何选?性能与价格怎样平衡?  微信h5制作网站有哪些,免费微信H5页面制作工具?  建站之星伪静态规则如何正确配置?  Python多线程使用规范_线程安全解析【教程】  武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?  香港服务器租用费用高吗?如何避免常见误区?  盐城做公司网站,江苏电子版退休证办理流程?  ,南京靠谱的征婚网站?  网站制作网站,深圳做网站哪家比较好?  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  浙江网站制作公司有哪些,浙江栢塑信息技术有限公司定制网站做的怎么样?  建站为何优先选择香港服务器?  宿州网站制作公司兴策,安徽省低保查询网站?  建站之星客服服务时间及联系方式如何?  制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?  如何挑选优质建站一级代理提升网站排名?  如何选择长沙网站建站模板?H5响应式与品牌定制哪个更优?  制作销售网站教学视频,销售网站有哪些?  如何使用Golang安装API文档生成工具_快速生成接口文档  交易网站制作流程,我想开通一个网站,注册一个交易网址,需要那些手续?  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】  教育培训网站制作流程,请问edu教育网站的域名怎么申请?  购物网站制作公司有哪些,哪个购物网站比较好?  建站之星2.7模板快速切换与批量管理功能操作指南  建站之星后台密码遗忘?如何快速找回?  如何在Golang中使用replace替换模块_指定本地或远程路径 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。