spring的三种事务配置方式

  1. <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
  2. <property name="transactionManager" ref="transactionManager"></property>
  3. <property name="transactionAttributes">
  4. <props>
  5. <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
  6. <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
  7. <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
  8. <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
  9. <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
  10. <prop key="get*">PROPAGATION_NEVER</prop>
  11. </props>
  12. </property>
  13. </bean>
  14. <bean id="userDao" class="com.tristan.web.controller.dao.UserDAO">
  15. <property name="sessionFactory" ref="sessionFactory"></property>
  16. </bean>
  17. <bean id="userManagerBase" class="com.tristan.web.controller.service.UserManager">
  18. <property name="userDao" ref="userDao"></property>
  19. </bean>
  20. <bean id="userManager" parent="transactionBase">
  21. <property name="target" ref="userManagerBase"></property>
  22. </bean>
	<bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
		<property name="transactionManager" ref="transactionManager"></property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="get*">PROPAGATION_NEVER</prop>
			</props>
		</property>
	</bean>
	
	<bean id="userDao" class="com.tristan.web.controller.dao.UserDAO">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="userManagerBase" class="com.tristan.web.controller.service.UserManager">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="userManager" parent="transactionBase">
		<property name="target" ref="userManagerBase"></property>
	</bean>


第二种 tx:advice + aop:config
极大的减少了配置文件
Java代码 复制代码 收藏代码
  1. <tx:advice id="txAdvice" transaction-manager="txManager">
  2. <tx:attributes>
  3. <tx:method name="delete" propagation="REQUIRED" />
  4. <tx:method name="update" propagation="REQUIRED" />
  5. <tx:method name="*" read-only="true" />
  6. </tx:attributes>
  7. </tx:advice>
  8. <aop:config>
  9. <aop:pointcut expression="execution (* com.tristan.web.service.*.*(..))"
  10. id="services" />
  11. <aop:advisor advice-ref="txAdvice" pointcut-ref="services" />
  12. </aop:config>
  13. <context:component-scan base-package="com.tristan.web.service" />
  14. <context:component-scan base-package="com.tristan.web.dao" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值