Spring事务配置的五种方式
创始人
2024-04-04 11:20:44
0

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

总结如下:

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

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

具体如下图:

Spring事务配置 (2)


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

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

  1.  version="1.0" encoding="UTF-8"?> 
  2.  xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop                                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
  11.      id="sessionFactory"   
  12.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  13.          name="configLocation" value="classpath:hibernate.cfg.xml" />   
  14.          name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  15.        
  16.        
  17.      id="transactionManager" 
  18.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  19.          name="sessionFactory" ref="sessionFactory" /> 
  20.      
  21.      
  22.      id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> 
  23.          name="sessionFactory" ref="sessionFactory" /> 
  24.      
  25.      id="userDao"   
  26.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
  27.               
  28.             name="transactionManager" ref="transactionManager" />      
  29.          name="target" ref="userDaoTarget" />   
  30.           name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /> 
  31.            
  32.          name="transactionAttributes">   
  33.                
  34.                  key="*">PROPAGATION_REQUIRED 
  35.                
  36.            
  37.        
  38.  

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

  1.  version="1.0" encoding="UTF-8"?> 
  2.  xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop                                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
  11.      id="sessionFactory"   
  12.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  13.          name="configLocation" value="classpath:hibernate.cfg.xml" />   
  14.          name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  15.        
  16.        
  17.      id="transactionManager" 
  18.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  19.          name="sessionFactory" ref="sessionFactory" /> 
  20.      
  21.      id="transactionBase"   
  22.             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"   
  23.             lazy-init="true" abstract="true">   
  24.            
  25.          name="transactionManager" ref="transactionManager" />   
  26.            
  27.          name="transactionAttributes">   
  28.                
  29.                  key="*">PROPAGATION_REQUIRED   
  30.                
  31.            
  32.      
  33.      
  34.      id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> 
  35.          name="sessionFactory" ref="sessionFactory" /> 
  36.      
  37.      id="userDao" parent="transactionBase" >   
  38.          name="target" ref="userDaoTarget" />    
  39.      
  40.  

第三种方式:使用拦截器

  1.  version="1.0" encoding="UTF-8"?> 
  2.  xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop                                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
  11.      id="sessionFactory"   
  12.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  13.          name="configLocation" value="classpath:hibernate.cfg.xml" />   
  14.          name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  15.        
  16.        
  17.      id="transactionManager" 
  18.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  19.          name="sessionFactory" ref="sessionFactory" /> 
  20.        
  21.     
  22.      id="transactionInterceptor"   
  23.         class="org.springframework.transaction.interceptor.TransactionInterceptor">   
  24.          name="transactionManager" ref="transactionManager" />   
  25.            
  26.          name="transactionAttributes">   
  27.                
  28.                  key="*">PROPAGATION_REQUIRED   
  29.                
  30.            
  31.      
  32.      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
  33.          name="beanNames">   
  34.                
  35.                 *Dao 
  36.                
  37.            
  38.          name="interceptorNames">   
  39.                
  40.                 transactionInterceptor   
  41.                
  42.            
  43.        
  44.      
  45.      id="userDao" class="com.bluesky.spring.dao.UserDaoImpl"> 
  46.          name="sessionFactory" ref="sessionFactory" /> 
  47.      
  48.  

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

  1.  version="1.0" encoding="UTF-8"?> 
  2.  xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xmlns:tx="http://www.springframework.org/schema/tx" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop                                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.            http://www.springframework.org/schema/tx                                      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
  13.      /> 
  14.      base-package="com.bluesky" /> 
  15.      id="sessionFactory"   
  16.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  17.          name="configLocation" value="classpath:hibernate.cfg.xml" />   
  18.          name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  19.        
  20.        
  21.      id="transactionManager" 
  22.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  23.          name="sessionFactory" ref="sessionFactory" /> 
  24.      
  25.      id="txAdvice" transaction-manager="transactionManager"> 
  26.          
  27.              name="*" propagation="REQUIRED" /> 
  28.          
  29.      
  30.      
  31.          id="interceptorPointCuts" 
  32.             expression="execution(* com.bluesky.spring.dao.*.*(..))" /> 
  33.          advice-ref="txAdvice" 
  34.             pointcut-ref="interceptorPointCuts" />         
  35.            
  36.  

第五种方式:全注解

  1.  version="1.0" encoding="UTF-8"?> 
  2.  xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xmlns:tx="http://www.springframework.org/schema/tx" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop                                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.            http://www.springframework.org/schema/tx                                      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
  13.      /> 
  14.      base-package="com.bluesky" /> 
  15.      transaction-manager="transactionManager"/> 
  16.      id="sessionFactory"   
  17.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  18.          name="configLocation" value="classpath:hibernate.cfg.xml" />   
  19.          name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  20.        
  21.        
  22.      id="transactionManager" 
  23.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  24.          name="sessionFactory" ref="sessionFactory" /> 
  25.      
  26.  

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

  1. package com.bluesky.spring.dao;  
  2. import java.util.List;  
  3. import org.hibernate.SessionFactory;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  6. import org.springframework.stereotype.Component;  
  7. import com.bluesky.spring.domain.User;  
  8. @Transactional  
  9. @Component("userDao")  
  10. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {  
  11.     public List listUsers() {  
  12.         return this.getSession().createQuery("from User").list();  
  13.     }     

【编辑推荐】

  1. Spring.NET1.1.2发布
  2. Java的Spring框架概述
  3. 对于Struts和Spring两种MVC框架的比较
  4. Spring创始人:看衰Java EE
  5. 将Flex与Spring集成框架

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...