Spring事务配置的五种方式

  1. 总结如下:  
  2.   
  3.     Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。  
  4.   
  5.     DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。  
  6.   
  7.     具体如下图:  
  8.   
  9. Spring事务配置 (2)  
  10.   
  11.     根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:  
  12.   
  13.     第一种方式:每个Bean都有一个代理  
  14. <?xml version="1.0" encoding="UTF-8"?>  
  15. <beans xmlns="http://www.springframework.org/schema/beans"  
  16.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  17.     xmlns:context="http://www.springframework.org/schema/context"  
  18.     xmlns:aop="http://www.springframework.org/schema/aop"  
  19.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  20.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  21.            http://www.springframework.org/schema/context  
  22.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  23.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  24.   
  25.     <bean id="sessionFactory"   
  26.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  27.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  28.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  29.     </bean>   
  30.   
  31.     <!-- 定义事务管理器(声明式的事务) -->   
  32.     <bean id="transactionManager"  
  33.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  34.         <property name="sessionFactory" ref="sessionFactory" />  
  35.     </bean>  
  36.      
  37.     <!-- 配置DAO -->  
  38.     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">  
  39.         <property name="sessionFactory" ref="sessionFactory" />  
  40.     </bean>  
  41.      
  42.     <bean id="userDao"   
  43.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
  44.            <!-- 配置事务管理器 -->   
  45.            <property name="transactionManager" ref="transactionManager" />      
  46.         <property name="target" ref="userDaoTarget" />   
  47.          <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />  
  48.         <!-- 配置事务属性 -->   
  49.         <property name="transactionAttributes">   
  50.             <props>   
  51.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
  52.             </props>   
  53.         </property>   
  54.     </bean>   
  55. </beans>  
  56.   
  57.     第二种方式:所有Bean共享一个代理基类  
  58. <?xml version="1.0" encoding="UTF-8"?>  
  59. <beans xmlns="http://www.springframework.org/schema/beans"  
  60.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  61.     xmlns:context="http://www.springframework.org/schema/context"  
  62.     xmlns:aop="http://www.springframework.org/schema/aop"  
  63.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  64.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  65.            http://www.springframework.org/schema/context  
  66.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  67.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  68.   
  69.     <bean id="sessionFactory"   
  70.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  71.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  72.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  73.     </bean>   
  74.   
  75.     <!-- 定义事务管理器(声明式的事务) -->   
  76.     <bean id="transactionManager"  
  77.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  78.         <property name="sessionFactory" ref="sessionFactory" />  
  79.     </bean>  
  80.      
  81.     <bean id="transactionBase"   
  82.             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"   
  83.             lazy-init="true" abstract="true">   
  84.         <!-- 配置事务管理器 -->   
  85.         <property name="transactionManager" ref="transactionManager" />   
  86.         <!-- 配置事务属性 -->   
  87.         <property name="transactionAttributes">   
  88.             <props>   
  89.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  90.             </props>   
  91.         </property>   
  92.     </bean>     
  93.     
  94.     <!-- 配置DAO -->  
  95.     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">  
  96.         <property name="sessionFactory" ref="sessionFactory" />  
  97.     </bean>  
  98.      
  99.     <bean id="userDao" parent="transactionBase" >   
  100.         <property name="target" ref="userDaoTarget" />    
  101.     </bean>  
  102. </beans>  
  103.   
  104. 第三种方式:使用拦截器  
  105. <?xml version="1.0" encoding="UTF-8"?>  
  106. <beans xmlns="http://www.springframework.org/schema/beans"  
  107.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  108.     xmlns:context="http://www.springframework.org/schema/context"  
  109.     xmlns:aop="http://www.springframework.org/schema/aop"  
  110.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  111.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  112.            http://www.springframework.org/schema/context  
  113.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  114.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  115.   
  116.     <bean id="sessionFactory"   
  117.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  118.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  119.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  120.     </bean>   
  121.   
  122.     <!-- 定义事务管理器(声明式的事务) -->   
  123.     <bean id="transactionManager"  
  124.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  125.         <property name="sessionFactory" ref="sessionFactory" />  
  126.     </bean>   
  127.     
  128.     <bean id="transactionInterceptor"   
  129.         class="org.springframework.transaction.interceptor.TransactionInterceptor">   
  130.         <property name="transactionManager" ref="transactionManager" />   
  131.         <!-- 配置事务属性 -->   
  132.         <property name="transactionAttributes">   
  133.             <props>   
  134.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  135.             </props>   
  136.         </property>   
  137.     </bean>  
  138.        
  139.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
  140.         <property name="beanNames">   
  141.             <list>   
  142.                 <value>*Dao</value>  
  143.             </list>   
  144.         </property>   
  145.         <property name="interceptorNames">   
  146.             <list>   
  147.                 <value>transactionInterceptor</value>   
  148.             </list>   
  149.         </property>   
  150.     </bean>   
  151.    
  152.     <!-- 配置DAO -->  
  153.     <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">  
  154.         <property name="sessionFactory" ref="sessionFactory" />  
  155.     </bean>  
  156. </beans>  
  157.   
  158. 第四种方式:使用tx标签配置的拦截器  
  159. <?xml version="1.0" encoding="UTF-8"?>  
  160. <beans xmlns="http://www.springframework.org/schema/beans"  
  161.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  162.     xmlns:context="http://www.springframework.org/schema/context"  
  163.     xmlns:aop="http://www.springframework.org/schema/aop"  
  164.     xmlns:tx="http://www.springframework.org/schema/tx"  
  165.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  166.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  167.            http://www.springframework.org/schema/context  
  168.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  169.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  170.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  171.   
  172.     <context:annotation-config />  
  173.     <context:component-scan base-package="com.bluesky" />  
  174.   
  175.     <bean id="sessionFactory"   
  176.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  177.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  178.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  179.     </bean>   
  180.   
  181.     <!-- 定义事务管理器(声明式的事务) -->   
  182.     <bean id="transactionManager"  
  183.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  184.         <property name="sessionFactory" ref="sessionFactory" />  
  185.     </bean>  
  186.   
  187.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  188.         <tx:attributes>  
  189.             <tx:method name="*" propagation="REQUIRED" />  
  190.         </tx:attributes>  
  191.     </tx:advice>  
  192.      
  193.     <aop:config>  
  194.         <aop:pointcut id="interceptorPointCuts"  
  195.             expression="execution(* com.bluesky.spring.dao.*.*(..))" />  
  196.         <aop:advisor advice-ref="txAdvice"  
  197.             pointcut-ref="interceptorPointCuts" />         
  198.     </aop:config>       
  199. </beans>  
  200.   
  201. 第五种方式:全注解  
  202. <?xml version="1.0" encoding="UTF-8"?>  
  203. <beans xmlns="http://www.springframework.org/schema/beans"  
  204.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  205.     xmlns:context="http://www.springframework.org/schema/context"  
  206.     xmlns:aop="http://www.springframework.org/schema/aop"  
  207.     xmlns:tx="http://www.springframework.org/schema/tx"  
  208.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  209.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  210.            http://www.springframework.org/schema/context  
  211.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  212.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  213.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  214.   
  215.     <context:annotation-config />  
  216.     <context:component-scan base-package="com.bluesky" />  
  217.   
  218.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  219.   
  220.     <bean id="sessionFactory"   
  221.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  222.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  223.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  224.     </bean>   
  225.   
  226.     <!-- 定义事务管理器(声明式的事务) -->   
  227.     <bean id="transactionManager"  
  228.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  229.         <property name="sessionFactory" ref="sessionFactory" />  
  230.     </bean>  
  231.      
  232. </beans>  
  233.   
  234. 此时在DAO上需加上@Transactional注解,如下:  
  235. package com.bluesky.spring.dao;  
  236.   
  237. import java.util.List;  
  238.   
  239. import org.hibernate.SessionFactory;  
  240. import org.springframework.beans.factory.annotation.Autowired;  
  241. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  242. import org.springframework.stereotype.Component;  
  243.   
  244. import com.bluesky.spring.domain.User;  
  245.   
  246. @Transactional  
  247. @Component("userDao")  
  248. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {  
  249.   
  250.     public List<User> listUsers() {  
  251.         return this.getSession().createQuery("from User").list();  
  252.     }  
  253.      
  254.      
  255. }  
Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值