Spring配置文件

 Spring配置文件中關於事務配置總是由三個組成部分,分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分。

    DataSource、TransactionManager這兩部分只是會根據數據訪問方式有所變化,比如使用Hibernate進行數據訪問時,DataSource實際爲SessionFactory,TransactionManager的實現爲HibernateTransactionManager。

 

根據代理機制的不同,總結了五種Spring事務的配置方式,配置文件如下:

 第一種方式:每個Bean都有一個代理

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " >
  3.  
  4.     <bean id="sessionFactory"   
  5.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  6.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  7.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  8.     </bean>   
  9.  
  10.     <!-- 定義事務管理器(聲明式的事務) -->   
  11.     <bean id="transactionManager" 
  12.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  13.         <property name="sessionFactory" ref="sessionFactory" /> 
  14.     </bean> 
  15.      
  16.     <!-- 配置DAO --> 
  17.     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> 
  18.         <property name="sessionFactory" ref="sessionFactory" /> 
  19.     </bean> 
  20.      
  21.     <bean id="userDao"   
  22.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
  23.            <!-- 配置事務管理器 -->   
  24.            <property name="transactionManager" ref="transactionManager" />      
  25.         <property name="target" ref="userDaoTarget" />   
  26.          <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /> 
  27.         <!-- 配置事務屬性 -->   
  28.         <property name="transactionAttributes">   
  29.             <props>   
  30.                 <prop key="*">PROPAGATION_REQUIRED</prop> 
  31.             </props>   
  32.         </property>   
  33.     </bean>   
  34. </beans> 

 第二種方式:所有Bean共享一個代理基類

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " >
  3.     <bean id="sessionFactory"   
  4.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  5.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  6.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  7.     </bean>   
  8.  
  9.     <!-- 定義事務管理器(聲明式的事務) -->   
  10.     <bean id="transactionManager" 
  11.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  12.         <property name="sessionFactory" ref="sessionFactory" /> 
  13.     </bean> 
  14.      
  15.     <bean id="transactionBase"   
  16.             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"   
  17.             lazy-init="true" abstract="true">   
  18.         <!-- 配置事務管理器 -->   
  19.         <property name="transactionManager" ref="transactionManager" />   
  20.         <!-- 配置事務屬性 -->   
  21.         <property name="transactionAttributes">   
  22.             <props>   
  23.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  24.             </props>   
  25.         </property>   
  26.     </bean>     
  27.     
  28.     <!-- 配置DAO --> 
  29.     <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> 
  30.         <property name="sessionFactory" ref="sessionFactory" /> 
  31.     </bean> 
  32.      
  33.     <bean id="userDao" parent="transactionBase" >   
  34.         <property name="target" ref="userDaoTarget" />    
  35.     </bean> 
  36. </beans> 

第三種方式:使用攔截器

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " >
  3.     <bean id="sessionFactory"   
  4.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  5.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  6.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  7.     </bean>   
  8.  
  9.     <!-- 定義事務管理器(聲明式的事務) -->   
  10.     <bean id="transactionManager" 
  11.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  12.         <property name="sessionFactory" ref="sessionFactory" /> 
  13.     </bean>  
  14.     
  15.     <bean id="transactionInterceptor"   
  16.         class="org.springframework.transaction.interceptor.TransactionInterceptor">   
  17.         <property name="transactionManager" ref="transactionManager" />   
  18.         <!-- 配置事務屬性 -->   
  19.         <property name="transactionAttributes">   
  20.             <props>   
  21.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  22.             </props>   
  23.         </property>   
  24.     </bean> 
  25.        
  26.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
  27.         <property name="beanNames">   
  28.             <list>   
  29.                 <value>*Dao</value> 
  30.             </list>   
  31.         </property>   
  32.         <property name="interceptorNames">   
  33.             <list>   
  34.                 <value>transactionInterceptor</value>   
  35.             </list>   
  36.         </property>   
  37.     </bean>   
  38.    
  39.     <!-- 配置DAO --> 
  40.     <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl"> 
  41.         <property name="sessionFactory" ref="sessionFactory" /> 
  42.     </bean> 
  43. </beans> 


第四種方式:使用tx標籤配置的攔截器




 

  1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " >
  2.     <context:annotation-config /> 
  3.     <context:component-scan base-package="com.bluesky" /> 
  4.  
  5.     <bean id="sessionFactory"   
  6.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  7.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  8.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  9.     </bean>   
  10.  
  11.     <!-- 定義事務管理器(聲明式的事務) -->   
  12.     <bean id="transactionManager" 
  13.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  14.         <property name="sessionFactory" ref="sessionFactory" /> 
  15.     </bean> 
  16.  
  17.     <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
  18.         <tx:attributes> 
  19.             <tx:method name="*" propagation="REQUIRED" /> 
  20.         </tx:attributes> 
  21.     </tx:advice> 
  22.      
  23.     <aop:config> 
  24.         <aop:pointcut id="interceptorPointCuts" 
  25.             expression="execution(* com.bluesky.spring.dao.*.*(..))" /> 
  26.         <aop:advisor advice-ref="txAdvice" 
  27.             pointcut-ref="interceptorPointCuts" />         
  28.     </aop:config>       
  29. </beans> 

第五種方式:全註解


 

  1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " >
  2.     <context:annotation-config /> 
  3.     <context:component-scan base-package="com.bluesky" /> 
  4.  
  5.     <tx:annotation-driven transaction-manager="transactionManager"/> 
  6.  
  7.     <bean id="sessionFactory"   
  8.             class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  9.         <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  10.         <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> 
  11.     </bean>   
  12.  
  13.     <!-- 定義事務管理器(聲明式的事務) -->   
  14.     <bean id="transactionManager" 
  15.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  16.         <property name="sessionFactory" ref="sessionFactory" /> 
  17.     </bean> 
  18.      
  19. </beans> 



此時在DAO上需加上@Transactional註解,如下:




 

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


 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章