日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/1/21 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring tx:advice 和 aop:config 配置事务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u010741376/article/details/46584463 spring tx:advice 和 aop:config 配置事務?
關鍵字: eclipse不能識別<tx:advice/>標簽?
<?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/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 "?
>?

<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。?

具體表現如下:?

<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 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/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 ">?

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


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 將不觸發事務回滾?

總結

以上是生活随笔為你收集整理的spring tx:advice 和 aop:config 配置事务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。