spring tx:advice 和 aop:config 配置事務

來源:互聯網
上載者:User
關鍵字: spring配置事務

Xml代碼  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.                      http://www.springframework.org/schema/beans/spring-beans.xsd   
  10.                      http://www.springframework.org/schema/tx   
  11.                      http://www.springframework.org/schema/tx/spring-tx.xsd   
  12.                      http://www.springframework.org/schema/aop   
  13.                      http://www.springframework.org/schema/aop/spring-aop.xsd "   
  14. >  
  15.   
  16. <bean id="transactionManager"  
  17.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"  
  18.         abstract="false" lazy-init="default" autowire="default"  
  19.         dependency-check="default">  
  20.         <property name="sessionFactory">  
  21.             <ref bean="sessionFactory" />  
  22.         </property>  
  23.     </bean>  
  24.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  25.         <tx:attributes>  
  26.             <tx:method name="add*" propagation="REQUIRED" />  
  27.             <tx:method name="delete*" propagation="REQUIRED" />  
  28.             <tx:method name="update*" propagation="REQUIRED" />  
  29.             <tx:method name="add*" propagation="REQUIRED" />  
  30.             <!-- <tx:method name="*" propagation="true" />-->  
  31.         </tx:attributes>  
  32.   
  33.     </tx:advice>  
  34.   
  35.     <aop:config>  
  36.         <aop:pointcut id="allManagerMethod"  
  37.             expression="execution(* com.service.*.*(..))" />  
  38.         <aop:advisor advice-ref="txAdvice"  
  39.             pointcut-ref="allManagerMethod" />  
  40.     </aop:config>  
  41. </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: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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop                     http://www.springframework.org/schema/aop/spring-aop.xsd "><bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager"        abstract="false" lazy-init="default" autowire="default"        dependency-check="default">        <property name="sessionFactory">            <ref bean="sessionFactory" />        </property>    </bean>    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="add*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />            <tx:method name="add*" propagation="REQUIRED" />            <!-- <tx:method name="*" propagation="true" />-->        </tx:attributes>    </tx:advice>    <aop:config>        <aop:pointcut id="allManagerMethod"            expression="execution(* com.service.*.*(..))" />        <aop:advisor advice-ref="txAdvice"            pointcut-ref="allManagerMethod" />    </aop:config></beans>

Eclipse不能識別<tx:advice/>標籤

在開發Spring的過程中,有時會出現Eclipse不能識別<tx:advice/>標籤。

提示出現以下錯誤:

The prefix "tx" for element "tx:advice" is not bound

這個錯誤的原因很簡單是:

我們在定義申明AOP的時候。。沒有載入schema。

具體表現如下: Xml代碼  

  1. <beans>  
  2.   
  3. <tx:advice id="txAdvice" transaction-manager="transactionManager">    
  4.      <tx:attributes>    
  5.          <tx:method name="get*" read-only="true"/>    
  6.          <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/>    
  7.      </tx:attributes>    
  8. </tx:advice>  
  9.   
  10. <!-- aop代理設定-->    
  11. <aop:config proxy-target-class="true">      
  12. ....   
  13.   
  14. </aop:config>  
  15.   
  16. </beans>  
<beans><tx:advice id="txAdvice" transaction-manager="transactionManager">      <tx:attributes>          <tx:method name="get*" read-only="true"/>          <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/>      </tx:attributes> </tx:advice><!-- aop代理設定--> <aop:config proxy-target-class="true">   ....</aop:config></beans>

這時會拋出異常不認<TX>標籤。。起先還以為是沒有載入JAR包呢。。

後來讀AOP文檔才發現<beans>中要加入“xmlns:aop”的命名申明,並在“xsi:schemaLocation”中指定aop配置的schema的地址

設定檔如下: Xml代碼  

  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:aop="http://www.springframework.org/schema/aop "  
  5. xmlns:tx="http://www.springframework.org/schema/tx "  
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.                      http://www.springframework.org/schema/beans/spring-beans.xsd   
  8.                      http://www.springframework.org/schema/tx   
  9.                      http://www.springframework.org/schema/tx/spring-tx.xsd   
  10.                      http://www.springframework.org/schema/aop   
  11.                      http://www.springframework.org/schema/aop/spring-aop.xsd ">  
<?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:aop="http://www.springframework.org/schema/aop "xmlns:tx="http://www.springframework.org/schema/tx "xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop                     http://www.springframework.org/schema/aop/spring-aop.xsd ">

這些才是最關鍵的地方。。後面的配置不變。。。。

Spring使用 <tx:advice>和 <aop:config> 用來配置事務,具體如何配置你可以參考Spring文檔。

我解釋一下(* com.evan.crm.service.*.*(..))中幾個萬用字元的含義:

|第一個 * —— 通配 任意傳回值類型|
|第二個 * —— 通配 包com.evan.crm.service下的任意class|
|第三個 * —— 通配 包com.evan.crm.service下的任意class的任意方法|
|第四個 .. —— 通配 方法可以有0個或多個參數|

綜上:包com.evan.crm.service下的任意class的具有任意傳回值類型、任意數目參數和任意名稱的方法
<tx:advice/> 有關的設定

這一節裡將描述通過 <tx:advice/> 標籤來指定不同的事務性設定。預設的 <tx:advice/> 設定如下:

事務傳播設定是 REQUIRED

隔離等級是 DEFAULT

事務是 讀/寫

事務逾時預設是依賴於事務系統的,或者事務逾時沒有被支援。

任何 RuntimeException 將觸發交易回復,但是任何 checked Exception 將不觸發交易回復

這些預設的設定當然也是可以被改變的。 <tx:advice/> 和 <tx:attributes/> 標籤裡的 <tx:method/> 各種屬性設定總結如下:
表 9.1. <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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.