---
title: "spring中配置使用事务"
author: "pignut_wang"
date: "2007-02-18T14:00:48Z"
tags: ["AOP", "Bean", "Spring"]
slug: "spring"
draft: false
lastmod: "2007-02-18T14:00:48Z"
id: 54785
url: "https://pignut-wang.iteye.com/blog/54785"
---

使用如下配置方式实现:

无论在方法中发生任何异常,只要方法中抛出一个Exception,包括是自己设置抛出的异常,事务都进行回滚

对比1.1与2.0中事务的配置方法

配置文件如下 1.1

01. &lt;bean id="baseTxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"           abstract="true"&gt;
02. &lt;property name="transactionManager" ref="transactionManager"/&gt;
03. &lt;property name="proxyTargetClass" value="true"/&gt;
04. &lt;property name="transactionAttributes"&gt;
05. &lt;props&gt;
06. &lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly &lt;/prop&gt;
07. &lt;prop key="find\*"&gt;PROPAGATION\_REQUIRED,readOnly &lt;/prop&gt;
08. &lt;prop key="save\*"&gt;PROPAGATION\_REQUIRED,-Exception &lt;/prop&gt;
09. &lt;prop key="remove\*"&gt;PROPAGATION\_REQUIRED ,-Exception &lt;/prop&gt;
10. props&gt;
11. property&gt;
12. &lt;property name="preInterceptors"&gt;
13. &lt;list&gt;
14. &lt;ref bean="methodSecurityInterceptor"/&gt;
15. list&gt;
16. property&gt;
17. bean&gt;
18. &lt;bean id="bookManager" parent="baseTxService"&gt;
19. &lt;property name="target"&gt;
20. &lt;bean class="org.springside.bookstore.admin.manager.BookManager"/&gt;
21. property&gt;
22. bean&gt;

 

spring 2.0 中事务配置方法

01. &lt;aop:config proxy-target-class="true"&gt;
02. &lt;aop:advisor pointcut="execution(* \*..\*Manager.\*(..))" advice-ref="txAdvice"/&gt;
03. &lt;aop:advisor pointcut="execution(* \*..\*Manager.save(..))" advice-ref="fooAdvice"/&gt; aop:config&gt;
04. &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
05. &lt;tx:attributes&gt;
06. &lt;tx:method name="save\*"/&gt;
07. &lt;tx:method name="remove\*"/&gt;
08. &lt;tx:method name="\*" read-only="true"/&gt;
09. tx:attributes&gt;
10. tx:advice&gt;
11. &lt;bean id="bookManager" class="org.springside.bookstore.commons.service.BookManager"/&gt;