Spring

Spring配置事务主要有这2种方式。 
1、配置一个Spring提供的一个工厂类,然后将需要进行事务管理的bean放入工厂bean中进行委托代理。 

Xml代码   收藏代码
  1. <bean id="companyDAOProxy"    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  2.     <property name="transactionManager">  
  3.         <ref bean="transactionManager" />  
  4.     </property>  
  5.     <property name="target">  
  6.         <ref local="companyDAOTarget" />  
  7.     </property>  
  8.     <property name="transactionAttributes">  
  9.         <props>  
  10.         <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  11.         <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  12.         <prop key="find*">  
  13.         PROPAGATION_REQUIRED,readOnly  
  14.         </prop>  
  15.         </props>  
  16.     </property>  
  17. </bean>  
  18. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  19. <property name="dataSource">    
  20. <ref local="dataSource" />   
  21. </property>    
  22. </bean>    

2、采用AOP方式进行事务配置,注意在配置文件中要注明AOP schema的地址。 
Xml代码   收藏代码
  1. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  2. <property name="dataSource">    
  3. <ref local="dataSource" />   
  4. </property>    
  5. </bean>    
  6. <aop:config>  
  7. <aop:pointcut id="serviceMethods" expression="execution(* com.business.impl..*Service*.*(..))" />   
  8. <aop:advisor pointcut-ref="serviceMethods" advice-ref="txAdvice" />   
  9. </aop:config>  
  10. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  11. <tx:attributes>  
  12. <tx:method name="get*"    propagation="SUPPORTS" read-only="true" />  
  13. <tx:method name="load*"   propagation="SUPPORTS" read-only="true" />  
  14. </tx:attributes>  
  15. </tx:advice>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值