聲明式事務(XML)【理解】【重點】

思路分析:將編程式事務中的通用代碼抽取出來,製作成獨立的around通知使用AOP工作原理,將事務管理的代碼動態織入到原始方法中。由於該功能使用量較大,Spring已經將該通知製作完畢。

1.開啓tx命名空間

<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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

">

2.定義Spring提供的事務管理通知

<!-- 事務管理Advice,由Spring提供 -->

<tx:advice id="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:method name="transfer"/>

</tx:attributes>

</tx:advice>

注意:TxAdvice需要爲其指定一個事務管理器的Bean

<!-- 聲明事務管理器的Bean,該Bean依賴數據源對象 -->

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property>

</bean>

3.設置AOP

<aop:config>

<aop:advisor advice-ref="txAdvice" pointcut="execution(void *..*.*.trans*(..))"/>

</aop:config>

4.將原始編程式事務控制中的控制代碼刪除

5.advice的參數

tx:advice:定義事務管理的通知(環繞通知)

transaction-manager:聲明事務管理的實現類

tx:method:定義參與事務管理的方法

7.事務傳播行爲 配置的是事務協調員(事務加入者)針對事務管理員(事務發起者)所攜帶的事務的處理態度 事務管理者(業務) 事務協調員(數據) REQUIRED T1 T1 無 T2 REQUIRES_NEW T1 T2 無 T1 SUPPORTS NOT_SUPPORTED MANDATORY NEVER NESTED T1 無 T1 無 T1 無 T1 無 savePoint()

T1 無 無 無 T1 錯誤 錯誤 OK

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