javaweb學習總結(四)spring配置Transaction的五種方式

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

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

具體如下圖:

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

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

[java] view plain copy
 print?
  1. <?xml version="1.0"encoding="UTF-8"?>   
  2.  <beans 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.   
  12.  <bean id="sessionFactory"   
  13.  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  14.  <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  15.  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />   
  16.  </bean>   
  17.   
  18.  <!-- 定義事務管理器(聲明式的事務) -->   
  19.  <bean id="transactionManager"  
  20.  class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  21.  <property name="sessionFactory" ref="sessionFactory" />   
  22.  </bean>   
  23.    
  24.  <!-- 配置DAO -->   
  25.  <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">  
  26.  <property name="sessionFactory" ref="sessionFactory" />   
  27.  </bean>   
  28.    
  29.  <bean id="userDao"   
  30.  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
  31.  <!-- 配置事務管理器 -->   
  32.  <property name="transactionManager" ref="transactionManager" />   
  33.  <property name="target" ref="userDaoTarget" />   
  34.  <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />   
  35.  <!-- 配置事務屬性 -->   
  36.  <property name="transactionAttributes">   
  37.  <props>   
  38.  <prop key="*"> PROPAGATION_REQUIRED </prop>   
  39.  </props>   
  40.  </property>   
  41.  </bean>   
  42.  </beans>  

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

[java] view plain copy
 print?
  1. <?xml version="1.0"encoding="UTF-8"?>   
  2.  <beans 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.   
  12.  <bean id="sessionFactory"   
  13.  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  14.  <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  15.  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />   
  16.  </bean>   
  17.   
  18.  <!-- 定義事務管理器(聲明式的事務) -->   
  19.  <bean id="transactionManager"  
  20.  class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  21.  <property name="sessionFactory" ref="sessionFactory" />   
  22.  </bean>   
  23.    
  24.  <bean id="transactionBase"   
  25.  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"   
  26.  lazy-init="true" abstract="true">   
  27.  <!-- 配置事務管理器 -->   
  28.  <property name="transactionManager" ref="transactionManager" />   
  29.  <!-- 配置事務屬性 -->   
  30.  <property name="transactionAttributes">   
  31.  <props>   
  32.  <prop key="*">PROPAGATION_REQUIRED </prop>   
  33.  </props>   
  34.  </property>   
  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" parent="transactionBase">   
  43.  <property name="target" ref="userDaoTarget" />   
  44.  </bean>   
  45.  </beans>  

第三種方式:使用攔截器

[java] view plain copy
 print?
  1. <?xml version="1.0"encoding="UTF-8"?>   
  2.  <beans 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.   
  12.  <bean id="sessionFactory"   
  13.  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  14.  <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  15.  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />   
  16.  </bean>   
  17.   
  18.  <!-- 定義事務管理器(聲明式的事務) -->   
  19.  <bean id="transactionManager"  
  20.  class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  21.  <property name="sessionFactory" ref="sessionFactory" />   
  22.  </bean>   
  23.    
  24.  <bean id="transactionInterceptor"   
  25.  class="org.springframework.transaction.interceptor.TransactionInterceptor">   
  26.  <property name="transactionManager" ref="transactionManager" />   
  27.  <!-- 配置事務屬性 -->   
  28.  <property name="transactionAttributes">   
  29.  <props>   
  30.  <prop key="*">PROPAGATION_REQUIRED </prop>   
  31.  </props>   
  32.  </property>   
  33.  </bean>   
  34.    
  35.  <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
  36.  <property name="beanNames">   
  37.  <list>   
  38.  <value> *Dao </value>   
  39.  </list>   
  40.  </property>   
  41.  <property name="interceptorNames">   
  42.  <list>   
  43.  <value> transactionInterceptor </value>   
  44.  </list>   
  45.  </property>   
  46.  </bean>   
  47.    
  48.  <!-- 配置DAO -->   
  49.  <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">  
  50.  <property name="sessionFactory" ref="sessionFactory" />   
  51.  </bean>   
  52.  </beans>  

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

[java] view plain copy
 print?
  1. <?xml version="1.0"encoding="UTF-8"?>   
  2.  <beans 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.  <context:annotation-config />   
  15.  <context:component-scan base-package="com.bluesky" />   
  16.   
  17.  <bean id="sessionFactory"   
  18.  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  19.  <property name="configLocation" value="classpath:hibernate.cfg.xml" />   
  20.  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />   
  21.  </bean>   
  22.   
  23.  <!-- 定義事務管理器(聲明式的事務) -->   
  24.  <bean id="transactionManager"  
  25.  class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  26.  <property name="sessionFactory" ref="sessionFactory" />   
  27.  </bean>   
  28.   
  29.  <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  30.  <tx:attributes>   
  31.  <tx:method name="*" propagation="REQUIRED" />   
  32.  </tx:attributes>   
  33.  </tx:advice>   
  34.    
  35.  <aop:config>   
  36.  <aop:pointcut id="interceptorPointCuts"  
  37.  expression="execution(*com.bluesky.spring.dao.*.*(..))" />   
  38.  <aop:advisor advice-ref="txAdvice"  
  39.  pointcut-ref="interceptorPointCuts" />   
  40.  </aop:config>   
  41.  </beans>  

Spring使用 <tx:advice>和 <aop:config> 用來配置事務,具體如何配置你可以參考Spring文檔。
我解釋一下execution(*com.bluesky.spring.dao.*.*(..))中幾個通配符的含義:
第一個 * —— 通配 任意返回值類型
第二個 * —— 通配 包com.bluesky.spring.dao下的任意class
第三個 * —— 通配 包com.bluesky.spring.dao下的任意class的任意方法
第四個 .. —— 通配 方法可以有0個或多個參數

綜上:包com.bluesky.spring.dao下的任意class的具有任意返回值類型、任意數目參數和任意名稱的方法<tx:advice/> 有關的設置這一節裏將描述通過 <tx:advice/> 標籤來指定不同的事務性設置。默認的 <tx:advice/> 設置如下:

  • 事務傳播設置是 REQUIRED

  • 隔離級別是 DEFAULT

  • 事務是 讀/寫

  • 事務超時默認是依賴於事務系統的,或者事務超時沒有被支持。

  • 任何 RuntimeException 將觸發事務回滾,但是任何 checked Exception 將不觸發事務回滾

這些默認的設置當然也是可以被改變的。 <tx:advice/> 和 <tx:attributes/> 標籤裏的 <tx:method/> 各種屬性設置總結如下:

 屬性 是否需要? 默認值 描述

name  

與事務屬性關聯的方法名。通配符(*)可以用來指定一批關聯到相同的事務屬性的方法。 如:'get*''handle*''on*Event'等等。

propagation REQUIRED 事務傳播行爲
isolation DEFAULT 事務隔離級別
timeout -1 事務超時的時間(以秒爲單位)
read-only false 事務是否只讀?
rollback-for  

將被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException,ServletException'

no-rollback-for  

 被觸發進行回滾的 Exception(s);以逗號分開。 如:'com.foo.MyBusinessException

 

第五種方式:全註解

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