Spring2.0 声明式事务配置

本文介绍如何使用Spring框架配置事务管理,包括定义事务管理器、指定事务处理的Bean及使用AOP实现事务控制的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- 声明一个事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
 </bean>
 <!-- 声明一个要进行事务处理的Bean -->
<bean id="petStore" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
  <property name="accountDao" ref="accountDao"/>
  <property name="categoryDao" ref="categoryDao"/>
  <property name="productDao" ref="productDao"/>
  <property name="itemDao" ref="itemDao"/>
  <property name="orderDao" ref="orderDao"/>
 </bean>
<!-- 下面的两处配置,才是整个事务AOP的核心,在1.2.8版本中,通过FactoryBean把事务对象(dataSource),与需要进行事务控制的对象PetStoreImpl串起来,对PetStoreImpl有侵入性 -->
 <!-- 而在之前的两处配置中,事务对象(dataSource)与需要进行事务控制的对象PetStoreImpl没有什么关系,它们的关系全部体现在下面的两处配置中 -->
<!-- 配置文件中各个属性的含义参考:http://www.redsaga.com/spring_ref/2.0/html/aop.html#aop-schema -->

 <aop:config>
  <aop:advisor pointcut="execution(* *..PetStoreFacade.*(..))" advice-ref="txAdvice"/> 
 </aop:config>
 
 <!--下面的transaction-manager属性原配置中没有,如果缺少此配置,默认值就是“transactionManager”在此加上,让人看的更明白。-->
 <!-- 参考 http://blog.xmnow.cn/doc/cn/spring2.0-reference_final_zh_cn/ch09s05.html -->
 <tx:advice id="txAdvice"  transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="insert*"/>
   <tx:method name="update*"/>
   <tx:method name="*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值