---
title: "spring tx:advice 和 aop:config 配置事务"
author: "xiexd"
date: "2010-09-25T17:49:58Z"
tags: ["AOP", "Bean", "Eclipse", "Spring", "XML"]
categories: ["企业架构"]
slug: "spring-tx-advice-aop-config"
draft: false
lastmod: "2010-09-25T17:49:58Z"
id: 771353
url: "https://parkmy.iteye.com/blog/771353"
---

Xml代码 [![复制代码](http://baobao707.iteye.com/images/icon_copy.gif "spring <wbr>tx:advice <wbr>和 <wbr>aop:config <wbr>配置事务")](http://baobao707.iteye.com/blog/415446 "复制代码")

01. **&lt;? xml**   version = "1.0"   encoding = "UTF-8" **?&gt;**
02. **&lt; beans**   xmlns = "http://www.springframework.org/schema/beans"
03. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
04. xmlns: aop = "http://www.springframework.org/schema/ aop "
05. xmlns:tx = "http://www.springframework.org/schema/tx"
06. xmlns:context = "http://www.springframework.org/schema/context"
07. xsi:schemaLocation ="http://www.springframework.org/schema/beans
08. http://www.springframework.org/schema/beans/spring -beans.xsd
09. http://www.springframework.org/schema/tx
10. http://www.springframework.org/schema/tx/spring -tx.xsd
11. http://www.springframework.org/schema/aop
12. http://www.springframework.org/schema/aop /spring -aop .xsd "
13. **&gt;**
14. **&lt; bean**   id = "transactionManager"
15. class = "org.springframework.orm.hibernate3.HibernateTransactionManager"
16. abstract = "false"   lazy-init = "default"   autowire = "default"
17. dependency-check = "default" **&gt;**
18. **&lt; property**   name = "sessionFactory" **&gt;**
19. **&lt; ref**   bean = "sessionFactory"   **/&gt;**
20. **&lt;/ property &gt;**
21. **&lt;/ bean &gt;**
22. **&lt; tx:advice**   id = "txAdvice"   transaction-manager = "transactionManager" **&gt;**
23. **&lt; tx:attributes &gt;**
24. **&lt; tx:method**   name = "add\*"   propagation = "REQUIRED"   **/&gt;**
25. **&lt; tx:method**   name = "delete\*"   propagation = "REQUIRED"   **/&gt;**
26. **&lt; tx:method**   name = "update\*"   propagation = "REQUIRED"   **/&gt;**
27. **&lt; tx:method**   name = "add\*"   propagation = "REQUIRED"   **/&gt;**
28. &lt;!-- &lt;tx:method name="\*" propagation="true" /&gt;--&gt;
29. **&lt;/ tx:attributes &gt;**
30. **&lt;/ tx:advice &gt;**
31. **&lt; aop :config &gt;**
32. **&lt; aop :pointcut**   id = "allManagerMethod"
33. expression\_r = "execution(* com.service.\*.\*(..))"   **/&gt;**
34. **&lt; aop :advisor**   advice-ref = "txAdvice"
35. pointcut-ref = "allManagerMethod"   **/&gt;**
36. **&lt;/ aop :config &gt;**
37. **&lt;/ beans &gt;**

```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop

="http://www.springframework.org/schema/aop

"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring

-beans.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring

-tx.xsd
                     http://www.springframework.org/schema/aop


                     http://www.springframework.org/schema/aop

/spring

-aop

.xsd "
>

<bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        abstract="false" lazy-init="default" 
        dependency-check="default">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <!-- <tx:method name="*" propagation="true" />-->
        </tx:attributes>

    </tx:advice>

    <aop

:config>
        <aop

:pointcut id="allManagerMethod"
            expression_r="execution(* com.service.*.*(..))" />
        <aop

:advisor advice-ref="txAdvice"
            pointcut-ref="allManagerMethod" />
    </aop


:config>
</beans>
```

Eclipse不能识别&lt;tx:advice/&gt;标签

在开发Spring 的过程中，有时会出现Eclipse不能识别&lt;tx:advice/&gt;标签。

提示出现以下错误：

The prefix "tx" for element "tx:advice" is not bound

这个错误的原因很简单是：

我们在定义申明AOP 的时候。。没有加载schema。

具体表现如下：

Xml代码 [![复制代码](http://baobao707.iteye.com/images/icon_copy.gif "spring <wbr>tx:advice <wbr>和 <wbr>aop:config <wbr>配置事务")](http://baobao707.iteye.com/blog/415446 "复制代码")

01. **&lt; beans &gt;**
02. **&lt; tx:advice**   id = "txAdvice"   transaction-manager = "transactionManager" **&gt;**
03. **&lt; tx:attributes &gt;**
04. **&lt; tx:method**   name = "get\*"   read-only = "true" **/&gt;**
05. **&lt; tx:method**   name = "\*"   propagation = "REQUIRES\_NEW"   rollback-for = "Exception" **/&gt;**
06. **&lt;/ tx:attributes &gt;**
07. **&lt;/ tx:advice &gt;**
08. &lt;!--  aop 代理设置--&gt;
09. **&lt; aop :config**   proxy-target-class = "true" **&gt;**
10. ....
11. **&lt;/ aop :config &gt;**
12. **&lt;/ beans &gt;**

```
<beans>

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
     <tx:attributes> 
         <tx:method name="get*" read-only="true"/> 
         <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/> 
     </tx:attributes> 
</tx:advice>

<!-- aop

代理设置--> 
<aop

:config proxy-target-class="true">   
....

</aop


:config>

</beans>
```

这时会抛出异常不认&lt;TX&gt;标签。。起先还以为是没有加载JAR包呢。。

后来读AOP 文档才发现&lt;beans&gt;中要加入“xmlns：aop ”的命名申明，并在“xsi：schemaLocation”中指定aop 配置的schema的地址

配置文件如下：

Xml代码 [![复制代码](http://baobao707.iteye.com/images/icon_copy.gif "spring <wbr>tx:advice <wbr>和 <wbr>aop:config <wbr>配置事务")](http://baobao707.iteye.com/blog/415446 "复制代码")

01. **&lt;? xml**   version = "1.0"   encoding = "UTF-8" **?&gt;**
02. **&lt; beans**   xmlns = "http://www.springframework.org/schema/beans "
03. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance "
04. xmlns: aop = "http://www.springframework.org/schema/ aop  "
05. xmlns:tx = "http://www.springframework.org/schema/tx "
06. xsi:schemaLocation ="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring -beans.xsd
08. http://www.springframework.org/schema/tx
09. http://www.springframework.org/schema/tx/spring -tx.xsd
10. http://www.springframework.org/schema/aop
11. http://www.springframework.org/schema/aop /spring -aop .xsd " **&gt;**

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xmlns:aop

="http://www.springframework.org/schema/aop

 "
xmlns:tx="http://www.springframework.org/schema/tx "
xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring

-beans.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring

-tx.xsd
                     http://www.springframework.org/schema/aop


                     http://www.springframework.org/schema/aop

/spring

-aop


.xsd ">
```

这些才是最关键的地方。。后面的配置不变。。。。

Spring 使用 &lt;tx:advice&gt;和 &lt;aop :config&gt; 用来配置事务，具体如何配置你可以参考Spring 文档。

我解释一下(* com.evan.crm.service.\*.\*(..))中几个通配符的含义：

|第一个 * —— 通配 任意返回值类型|  
|第二个 * —— 通配 包com.evan.crm.service下的任意class|  
|第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法|  
|第四个 .. —— 通配 方法可以有0个或多个参数|

综上：包com.evan.crm.service下的任意class的具有任意返回值类型、任意数目参数和任意名称的方法  
&lt;tx:advice/&gt; 有关的设置

这一节里将描述通过 &lt;tx:advice/&gt; 标签来指定不同的事务性设置。默认的 &lt;tx:advice/&gt; 设置如下：

事务传播设置是 REQUIRED

隔离级别是 DEFAULT

事务是 读/写

事务超时默认是依赖于事务系统的，或者事务超时没有被支持。

任何 RuntimeException 将触发事务回滚，但是任何 checked Exception 将不触发事务回滚

这些默认的设置当然也是可以被改变的。 &lt;tx:advice/&gt; 和 &lt;tx:attributes/&gt; 标签里的 &lt;tx:method/&gt; 各种属性设置总结如下：  
表 9.1. &lt;tx:method/&gt; 有关的设置

属性 是否需要？ 默认值 描述 name 是 与事务属性关联的方法名。通配符（\*）可以用来指定一批关联到相同的事务属性的方法。 如：'get\*'、'handle\*'、'on\*Event'等等。 propagation 不 REQUIRED 事务传播行为 isolation 不 DEFAULT 事务隔离级别 timeout 不 -1 事务超时的时间（以秒为单位） read-only 不 false 事务是否只读？ rollback-for 不 将被触发进行回滚的 Exception(s)；以逗号分开。 如：'com.foo.MyBusinessException,ServletException' no-rollback-for 不 不 被触发进行回滚的 Exception(s)；以逗号分开。 如：'com.foo.MyBusinessException