Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. 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. >
  14. <bean id="transactionManager"
  15. class="org.springframework.orm.hibernate3.HibernateTransactionManager"
  16. abstract="false" lazy-init="default" autowire="default"
  17. dependency-check="default">
  18. <property name="sessionFactory">
  19. <ref bean="sessionFactory" />
  20. </property>
  21. </bean>
  22. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  23. <tx:attributes>
  24. <tx:method name="add*" propagation="REQUIRED" />
  25. <tx:method name="delete*" propagation="REQUIRED" />
  26. <tx:method name="update*" propagation="REQUIRED" />
  27. <tx:method name="add*" propagation="REQUIRED" />
  28. <!-- <tx:method name="*" propagation="true" />-->
  29. </tx:attributes>
  30. </tx:advice>
  31. <aop:config>
  32. <aop:pointcut id="allManagerMethod"
  33. expression="execution(* com.service.*.*(..))" />
  34. <aop:advisor advice-ref="txAdvice"
  35. pointcut-ref="allManagerMethod" />
  36. </aop:config>
  37. </beans>

Eclipse不能识别<tx:advice/>标签

在开发Spring的过程中,有时会出现Eclipse不能识别<tx:advice/>标签。

提示出现以下错误:

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

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

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

具体表现如下:

Xml代码  
  1. <beans>
  2. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  3. <tx:attributes>
  4. <tx:method name="get*" read-only="true"/>
  5. <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/>
  6. </tx:attributes>
  7. </tx:advice>
  8. <!-- aop代理设置-->
  9. <aop:config proxy-target-class="true">
  10. ....
  11. </aop:config>
  12. </beans>

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

后来读AOP文档才发现<beans>中要加入“xmlns:aop”的命名申明,并在“xsi:schemaLocation”中指定aop配置的schema的地址

配置文件如下:

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans "
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
  4. xmlns:aop="http://www.springframework.org/schema/aop "
  5. xmlns:tx="http://www.springframework.org/schema/tx "
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/tx
  9. 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 ">

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

Spring使用 <tx:advice>和 <aop:config> 用来配置事务,具体如何配置你可以参考Spring文档。

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

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

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

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

事务传播设置是 REQUIRED

隔离级别是 DEFAULT

事务是 读/写

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

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

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

属性 是否需要? 默认值 描述
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

关键配置段的解释:

<tx:advice id="txAdvice" ><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/></tx:attributes>
</tx:advice> 

如上配置,是因为我们将事务应用在所有以save和modify开头的方法上,如saveMail(),saveMM(),modifyGG(),modifyBill()

意味着这些业务类的方法是标志了需要transaction(事务操作)的类。

<aop:config><aop:pointcut id="allServices"expression="execution(*com.mytransaction.service.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="allServices"/>
</aop:config>

而上面一段的意义是:将事务建议(transaction advice)配置给那些方法。

Spring AOP tx:advice相关推荐

  1. spring中tx:advice/tx:advice是什么意思?作用是什么?谁能简单说下

    spring中<tx:advice></tx:advice>是什么意思?作用是什么?谁能简单说下 2013-03-26 17:15暴力娃娃123 | 浏览 13763 次 &l ...

  2. spring中tx:advice中的tx是什么的缩写?

    spring中tx:advice中的tx是什么的缩写? transaction的缩写,即是跟事务相关的配置 tx 根据transaction的发音来缩写出来的

  3. Spring AOP 中 advice 的四种类型 before after throwing advice around

    Spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...

  4. Spring AOP 的 Advice 和 Advisor 有什么区别

    简单来说:Advice 是通知,Advisor 是增强器.(说了跟没说一样-) 使用 spring aop 要定义切面,切面里面有 通知 和 切点. 在项目启动的过程中,项目中的所有切面会被 Anno ...

  5. Spring AOP增强(Advice)

    Sring AOP通过PointCut来指定在那些类的那些方法上织入横切逻辑,通过Advice来指定在切点上具体做什么事情.如方法前做什么,方法后做什么,抛出异常做什么. 再来看一下图 定义Point ...

  6. Spring Aop——给Advice传递参数

    给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或ProceedingJoinPoint(Around Advice)参数外,还可以直接接收与切入点方 ...

  7. spring tx:advice 和 aop:config 配置事务

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010741376/article/details/46584463 spring tx:advic ...

  8. spring tx:advice 和 aop:config 配置事务 1

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. spring tx:advice事务配置

    http://www.cnblogs.com/rushoooooo/archive/2011/08/28/2155960.html 链接归纳的很详细 首先在/WEB-INF/applicationCo ...

最新文章

  1. learning to rank
  2. AI基础:机器学习简易入门
  3. html发送十六进制字符数组,十六进制数组怎么转换成字符串数组?
  4. 百度之星资格赛,hdu 4825 XOR SUM
  5. STM32工作笔记0075---UCOSIII任务管理(下)
  6. 智能优化算法:海洋捕食者算法
  7. 单片机音频谱曲软件_单片机音乐代码转换软件(Music Encode)
  8. STM32标准外设库(标准库)官网下载方法,附带2021最新标准固件库下载链接
  9. GMAP.NET应用及离线地图加载
  10. css将两张图片叠加(简易方法)
  11. JAVA 中 Redis与ehcache对比与使用
  12. adb连接小米电视,尝试去除开机广告失败补救方法
  13. 数字IC小白起步(一)
  14. Windows 2003和XP之间的异同
  15. 微信小程序 柱状图的使用
  16. 混沌与秩序服务器维护,混沌与秩序2切换角色以及服务器教程 注销账号教程
  17. 转载-ChatGPT在信息安全领域的应用前景
  18. itop4412 设备树 HDMI
  19. CCD机器视觉检测系统常规的工作流程
  20. Abp 实现通过手机号注册用户

热门文章

  1. Pygame显示文字
  2. oracle10g 学习笔记上
  3. 大阪第14天——韩寒同学语录
  4. 计算机毕业设计项目推荐 - 毕设开题选题
  5. python中assert是什么意思_你常常看到 Python 代码中的 assert 是个啥?
  6. 基于牛顿法的开平方实现
  7. 创业语录(转)(动态添加中)
  8. 晒晒我的“无法操作”的“发财计划”
  9. 1064 朋友数 (C++)
  10. html后代选择器的语法,[转]CSS子选择器与后代选择器