Spring AOP tx:advice
- <?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" autowire="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="execution(* com.service.*.*(..))" />
- <aop:advisor advice-ref="txAdvice"
- pointcut-ref="allManagerMethod" />
- </aop:config>
- </beans>
Eclipse不能识别<tx:advice/>标签
在开发Spring的过程中,有时会出现Eclipse不能识别<tx:advice/>标签。
提示出现以下错误:
The prefix "tx" for element "tx:advice" is not bound
这个错误的原因很简单是:
我们在定义申明AOP的时候。。没有加载schema。
具体表现如下:
- <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>
这时会抛出异常不认<TX>标签。。起先还以为是没有加载JAR包呢。。
后来读AOP文档才发现<beans>中要加入“xmlns:aop”的命名申明,并在“xsi:schemaLocation”中指定aop配置的schema的地址
配置文件如下:
- <?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使用 <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相关推荐
- spring中tx:advice/tx:advice是什么意思?作用是什么?谁能简单说下
spring中<tx:advice></tx:advice>是什么意思?作用是什么?谁能简单说下 2013-03-26 17:15暴力娃娃123 | 浏览 13763 次 &l ...
- spring中tx:advice中的tx是什么的缩写?
spring中tx:advice中的tx是什么的缩写? transaction的缩写,即是跟事务相关的配置 tx 根据transaction的发音来缩写出来的
- Spring AOP 中 advice 的四种类型 before after throwing advice around
Spring AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...
- Spring AOP 的 Advice 和 Advisor 有什么区别
简单来说:Advice 是通知,Advisor 是增强器.(说了跟没说一样-) 使用 spring aop 要定义切面,切面里面有 通知 和 切点. 在项目启动的过程中,项目中的所有切面会被 Anno ...
- Spring AOP增强(Advice)
Sring AOP通过PointCut来指定在那些类的那些方法上织入横切逻辑,通过Advice来指定在切点上具体做什么事情.如方法前做什么,方法后做什么,抛出异常做什么. 再来看一下图 定义Point ...
- Spring Aop——给Advice传递参数
给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或ProceedingJoinPoint(Around Advice)参数外,还可以直接接收与切入点方 ...
- spring tx:advice 和 aop:config 配置事务
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010741376/article/details/46584463 spring tx:advic ...
- spring tx:advice 和 aop:config 配置事务 1
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring tx:advice事务配置
http://www.cnblogs.com/rushoooooo/archive/2011/08/28/2155960.html 链接归纳的很详细 首先在/WEB-INF/applicationCo ...
最新文章
- learning to rank
- AI基础:机器学习简易入门
- html发送十六进制字符数组,十六进制数组怎么转换成字符串数组?
- 百度之星资格赛,hdu 4825 XOR SUM
- STM32工作笔记0075---UCOSIII任务管理(下)
- 智能优化算法:海洋捕食者算法
- 单片机音频谱曲软件_单片机音乐代码转换软件(Music Encode)
- STM32标准外设库(标准库)官网下载方法,附带2021最新标准固件库下载链接
- GMAP.NET应用及离线地图加载
- css将两张图片叠加(简易方法)
- JAVA 中 Redis与ehcache对比与使用
- adb连接小米电视,尝试去除开机广告失败补救方法
- 数字IC小白起步(一)
- Windows 2003和XP之间的异同
- 微信小程序 柱状图的使用
- 混沌与秩序服务器维护,混沌与秩序2切换角色以及服务器教程 注销账号教程
- 转载-ChatGPT在信息安全领域的应用前景
- itop4412 设备树 HDMI
- CCD机器视觉检测系统常规的工作流程
- Abp 实现通过手机号注册用户