###一、配置事務管理器###
在Bean配置文件中配置事務管理器。
需要注入數據源。
舉個例子:
<!-- 配置事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>12345
###二、 配置事務屬性###
需要先導入 tx 命名空間。
使用<tx:advice>
元素聲明事務通知,需要指定id屬性,以便AOP把通知和切入點關聯起來。
還需要指定transaction-manager
屬性,其值Bean配置文件中事務管理器的id屬性值。
在<tx:advice>
元素下聲明<tx:attributes>
元素,用于指定事務屬性。
在<tx:attributes>
元素下可以使用多個<tx:method>
元素指定多種事務屬性。
舉個例子:
<!-- 配置事務通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 根據方法名指定事務的屬性 -->
<tx:method name="BookShopXmlService" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>12345678910
###三、 配置事務切入點,把事務切入點與事務關聯起來###
在 <aop:config>
元素下,使用<aop:pointcut>
元素聲明切入點,其expression
屬性指定切入點表達式,還需要指定id屬性。
在 在<aop:config>
元素下,使用<aop:advisor>
元素聲明一個增強器,將事務通知和切入點關聯起來,使用 advice-ref
屬性指定事務通知,用pointcut-ref
屬性指定切入點。
舉個例子:
<!-- 配置事務切入點,以及把事務切入點和事務屬性關聯起來 -->
<aop:config>
<aop:pointcut expression="execution(* com.sqp.spring.service.*.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。