Spring事務配置(配置檔案的形式)
阿新 • • 發佈:2018-11-22
聲明瞭事務通知後, 就需要將它與切入點關聯起來. 由於事務通知是在 <aop:config> 元素外部宣告的, 所以它無法直接與切入點產生關聯. 所以必須在 <aop:config> 元素中宣告一個增強器通知與切入點關聯起來.
由於 Spring AOP 是基於代理的方法, 所以只能增強公共方法. 因此, 只有公有方法才能通過 Spring AOP 進行事務管理.
在xml檔案中的配置如下: <!-- 1. 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 2. 配置事務屬性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 根據方法名指定事務的屬性 --> <tx:method name="purchase" propagation="REQUIRES_NEW"/> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 3. 配置事務切入點, 以及把事務切入點和事務屬性關聯起來 --> <aop:config> <aop:pointcut expression="execution(* com.atguigu.spring.tx.xml.service.*.*(..))" id="txPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config>