Spring事务配置的五种方式

来源:互联网 发布:mac双系统怎么切换系统 编辑:程序博客网 时间:2024/06/15 17:24
Spring事务配置的五种方式:

总结如下:



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



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



    具体如下图:







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



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


Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.     xmlns:aop="http://www.springframework.org/schema/aop"  
  10.   
  11.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  12.   
  13.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.   
  15.            http://www.springframework.org/schema/context  
  16.   
  17.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  18.   
  19.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  20.   
  21.   
  22.   
  23.     <bean id="sessionFactory"    
  24.   
  25.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    
  26.   
  27.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />    
  28.   
  29.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  30.   
  31.     </bean>    
  32.   
  33.   
  34.   
  35.     <!-- 定义事务管理器(声明式的事务) -->    
  36.   
  37.     <bean id="transactionManager"  
  38.   
  39.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  40.   
  41.         <property name="sessionFactory" ref="sessionFactory" />  
  42.   
  43.     </bean>  
  44.   
  45.       
  46.   
  47.     <!-- 配置DAO -->  
  48.   
  49.     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">  
  50.   
  51.         <property name="sessionFactory" ref="sessionFactory" />  
  52.   
  53.     </bean>  
  54.   
  55.       
  56.   
  57.     <bean id="userDao"    
  58.   
  59.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">    
  60.   
  61.            <!-- 配置事务管理器 -->    
  62.   
  63.            <property name="transactionManager" ref="transactionManager" />       
  64.   
  65.         <property name="target" ref="userDaoTarget" />    
  66.   
  67.          <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />  
  68.   
  69.         <!-- 配置事务属性 -->    
  70.   
  71.         <property name="transactionAttributes">    
  72.   
  73.             <props>    
  74.   
  75.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
  76.   
  77.             </props>    
  78.   
  79.         </property>    
  80.   
  81.     </bean>    
  82.   
  83. </beans>  


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



Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.     xmlns:aop="http://www.springframework.org/schema/aop"  
  10.   
  11.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  12.   
  13.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.   
  15.            http://www.springframework.org/schema/context  
  16.   
  17.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  18.   
  19.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  20.   
  21.   
  22.   
  23.     <bean id="sessionFactory"    
  24.   
  25.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    
  26.   
  27.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />    
  28.   
  29.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  30.   
  31.     </bean>    
  32.   
  33.   
  34.   
  35.     <!-- 定义事务管理器(声明式的事务) -->    
  36.   
  37.     <bean id="transactionManager"  
  38.   
  39.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  40.   
  41.         <property name="sessionFactory" ref="sessionFactory" />  
  42.   
  43.     </bean>  
  44.   
  45.       
  46.   
  47.     <bean id="transactionBase"    
  48.   
  49.             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"    
  50.   
  51.             lazy-init="true" abstract="true">    
  52.   
  53.         <!-- 配置事务管理器 -->    
  54.   
  55.         <property name="transactionManager" ref="transactionManager" />    
  56.   
  57.         <!-- 配置事务属性 -->    
  58.   
  59.         <property name="transactionAttributes">    
  60.   
  61.             <props>    
  62.   
  63.                 <prop key="*">PROPAGATION_REQUIRED</prop>    
  64.   
  65.             </props>    
  66.   
  67.         </property>    
  68.   
  69.     </bean>      
  70.   
  71.      
  72.   
  73.     <!-- 配置DAO -->  
  74.   
  75.     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">  
  76.   
  77.         <property name="sessionFactory" ref="sessionFactory" />  
  78.   
  79.     </bean>  
  80.   
  81.       
  82.   
  83.     <bean id="userDao" parent="transactionBase" >    
  84.   
  85.         <property name="target" ref="userDaoTarget" />     
  86.   
  87.     </bean>  
  88.   
  89. </beans>  


第三种方式:使用拦截器



Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.     xmlns:aop="http://www.springframework.org/schema/aop"  
  10.   
  11.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  12.   
  13.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.   
  15.            http://www.springframework.org/schema/context  
  16.   
  17.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  18.   
  19.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  20.   
  21.   
  22.   
  23.     <bean id="sessionFactory"    
  24.   
  25.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    
  26.   
  27.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />    
  28.   
  29.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  30.   
  31.     </bean>    
  32.   
  33.   
  34.   
  35.     <!-- 定义事务管理器(声明式的事务) -->    
  36.   
  37.     <bean id="transactionManager"  
  38.   
  39.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  40.   
  41.         <property name="sessionFactory" ref="sessionFactory" />  
  42.   
  43.     </bean>   
  44.   
  45.      
  46.   
  47.     <bean id="transactionInterceptor"    
  48.   
  49.         class="org.springframework.transaction.interceptor.TransactionInterceptor">    
  50.   
  51.         <property name="transactionManager" ref="transactionManager" />    
  52.   
  53.         <!-- 配置事务属性 -->    
  54.   
  55.         <property name="transactionAttributes">    
  56.   
  57.             <props>    
  58.   
  59.                 <prop key="*">PROPAGATION_REQUIRED</prop>    
  60.   
  61.             </props>    
  62.   
  63.         </property>    
  64.   
  65.     </bean>  
  66.   
  67.         
  68.   
  69.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    
  70.   
  71.         <property name="beanNames">    
  72.   
  73.             <list>    
  74.   
  75.                 <value>*Dao</value>  
  76.   
  77.             </list>    
  78.   
  79.         </property>    
  80.   
  81.         <property name="interceptorNames">    
  82.   
  83.             <list>    
  84.   
  85.                 <value>transactionInterceptor</value>    
  86.   
  87.             </list>    
  88.   
  89.         </property>    
  90.   
  91.     </bean>    
  92.   
  93.     
  94.   
  95.     <!-- 配置DAO -->  
  96.   
  97.     <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">  
  98.   
  99.         <property name="sessionFactory" ref="sessionFactory" />  
  100.   
  101.     </bean>  
  102.   
  103. </beans>  

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



Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.     xmlns:aop="http://www.springframework.org/schema/aop"  
  10.   
  11.     xmlns:tx="http://www.springframework.org/schema/tx"  
  12.   
  13.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  14.   
  15.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  16.   
  17.            http://www.springframework.org/schema/context  
  18.   
  19.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  20.   
  21.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  22.   
  23.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  24.   
  25.   
  26.   
  27.     <context:annotation-config />  
  28.   
  29.     <context:component-scan base-package="com.bluesky" />  
  30.   
  31.   
  32.   
  33.     <bean id="sessionFactory"    
  34.   
  35.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    
  36.   
  37.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />    
  38.   
  39.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />  
  40.   
  41.     </bean>    
  42.   
  43.   
  44.   
  45.     <!-- 定义事务管理器(声明式的事务) -->    
  46.   
  47.     <bean id="transactionManager"  
  48.   
  49.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  50.   
  51.         <property name="sessionFactory" ref="sessionFactory" />  
  52.   
  53.     </bean>  
  54.   
  55.   
  56.   
  57.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  58.   
  59.         <tx:attributes>  
  60.   
  61.             <tx:method name="*" propagation="REQUIRED" />  
  62.   
  63.         </tx:attributes>  
  64.   
  65.     </tx:advice>  
  66.   
  67.       
  68.   
  69.     <aop:config>  
  70.   
  71.         <aop:pointcut id="interceptorPointCuts"  
  72.   
  73.             expression="execution(* com.bluesky.spring.dao.*.*(..))" />  
  74.   
  75.         <aop:advisor advice-ref="txAdvice"  
  76.   
  77.             pointcut-ref="interceptorPointCuts" />          
  78.   
  79.     </aop:config>        
  80.   
  81. </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注解,如下:



Java代码  收藏代码
  1. package com.bluesky.spring.dao;  
  2.   
  3.   
  4.   
  5. import java.util.List;  
  6.   
  7.   
  8.   
  9. import org.hibernate.SessionFactory;  
  10.   
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  14.   
  15. import org.springframework.stereotype.Component;  
  16.   
  17.   
  18.   
  19. import com.bluesky.spring.domain.User;  
  20.   
  21.   
  22.   
  23. @Transactional  
  24.   
  25. @Component("userDao")  
  26.   
  27. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {  
  28.   
  29.   
  30.   
  31.     public List<User> listUsers() {  
  32.   
  33.         return this.getSession().createQuery("from User").list();  
  34.   
  35.     }  
  36.   
  37.       
  38.   
  39.       
  40.   
  41. }  
原创粉丝点击