spring配置文件applicationContext.xml中的事务总结

 
spring配置文件中几种配置事务的方式:
第一种:通过aop管理用户的增删改 (aspectJ)
 <aop:config>
  <aop:advisor pointcut="execution(* *..OrderDao.*(..))"
   advice-ref="txAdvice" />
 </aop:config>
 <tx:advice id="txAdvice">
  <tx:attributes>
   <tx:method name="save*" />
   <tx:method name="update*" />
   <tx:method name="delete*" />
   <tx:method name="*" read-only="true" />
  </tx:attributes>
 </tx:advice>

第二种:通过拦截器
Dao实现类实例
<bean name="dao" class="org.itfuture.[url]www.dao.impl.OrderDaoImpl[/url]">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 事务bean
<bean id="baseproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transactionManager"/>
  <property name="target" ref="dao"/>//所在拦截的目标指向
  <property name="transactionAttributes">//拦截目标内的哪些方法
   <props>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
</bean>
创建orderservice子类(实现类)的实例,注入 orderDao的实现类orderimpl的实例
<bean name="orderservice"
 class="org.itfuture.[url]www.service.impl.OrderServiceImpl[/url]">
 <property name="dao" ref="baseproxy"></property>//引用baseproxy代理
</bean>

第三种配置:aop与interceptor相接合
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
     <property name="transactionManager" ref="transactionManager">//引用transactionManager
     </property>
     <property name="transactionAttributes">//定义哪些方法是要用到事务的,
       <props>
      <prop key="delete*">PROPAGATION_REQUIRED</prop>
      <prop key="save*">PROPAGATION_REQUIRED</prop>
      <prop key="update*">PROPAGATION_REQUIRED</prop>
      <prop key="add*">PROPAGATION_REQUIRED</prop>
      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
     </property>
 </bean>
 <bean id="auto" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="interceptorNames">
     <list>
        <value>transactionInterceptor</value>
     </list>
   </property>
 </bean>

第四种配置:个人认为比较好用这种
<bean id="dao" class="org.itfuture.[url]www.dao[/url]">//把所有的*dao上升到接口
  <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:config>
  <aop:advisor pointcut="execution(* *..interfaceDao.*(..))" advice-ref="txAdvice"/> //然后在拦截接口
 </aop:config>
 <tx:advice id="txAdvice">
  <tx:attributes>
   <tx:method name="insert*"/>//拦截接口中的以insert、update、update开头的所有方法
   <tx:method name="update*"/>
   <tx:method name="*" read-only="true"/>//其它方法,只read-only
  </tx:attributes>
 </tx:advice>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值