Spring事务配置的五种方式

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

成都创新互联-专业网站定制、快速模板网站建设、高性价比施甸网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式施甸网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖施甸地区。费用合理售后完善,十多年实体公司更值得信赖。

总结如下:

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

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

具体如下图:

Spring事务配置

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

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

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/context
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  9.     
  10.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  11.          
  12.         
  13.      
  14.      
  15.     
  16.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17.         
  18.     
  19.    
  20.     
  21.     
  22.         
  23.     
  24.    
  25.     
  26.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
  27.             
  28.                
  29.          
  30.          
  31.          
  32.          
  33.              
  34.                 PROPAGATION_REQUIRED
  35.              
  36.          
  37.      

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

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/context
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  9.     
  10.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  11.          
  12.         
  13.      
  14.      
  15.     
  16.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17.         
  18.     
  19.    
  20.     
  21.             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
  22.             lazy-init="true" abstract="true"> 
  23.          
  24.          
  25.          
  26.          
  27.              
  28.                 PROPAGATION_REQUIRED 
  29.              
  30.          
  31.        
  32.   
  33.     
  34.     
  35.         
  36.     
  37.    
  38.      
  39.           
  40.     

第三种方式:使用拦截器

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6.            http://www.springframework.org/schema/context
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  9.     
  10.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  11.          
  12.         
  13.      
  14.      
  15.     
  16.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  17.         
  18.      
  19.   
  20.     
  21.         class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
  22.          
  23.          
  24.          
  25.              
  26.                 PROPAGATION_REQUIRED 
  27.              
  28.          
  29.     
  30.      
  31.      
  32.          
  33.              
  34.                 *Dao
  35.              
  36.          
  37.          
  38.              
  39.                 transactionInterceptor 
  40.              
  41.          
  42.      
  43.  
  44.     
  45.     
  46.         
  47.     

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

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xmlns:tx="http://www.springframework.org/schema/tx"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7.            http://www.springframework.org/schema/context
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  11.     
  12.     
  13.     
  14.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  15.          
  16.         
  17.      
  18.      
  19.     
  20.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  21.         
  22.     
  23.     
  24.         
  25.             
  26.         
  27.     
  28.    
  29.     
  30.         
  31.             expression="execution(* com.bluesky.spring.dao.*.*(..))" />
  32.         
  33.             pointcut-ref="interceptorPointCuts" />       
  34.          

第五种方式:全注解

 
 
 
 
  1.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xmlns:tx="http://www.springframework.org/schema/tx"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7.            http://www.springframework.org/schema/context
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd
  9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  11.     
  12.     
  13.     
  14.     
  15.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  16.          
  17.         
  18.      
  19.      
  20.     
  21.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  22.         
  23.     
  24.    

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

 
 
 
 
  1. package com.dawnsky.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.     }
  14. }

网站题目:Spring事务配置的五种方式
网站路径:http://www.shufengxianlan.com/qtweb/news45/36545.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联