Spring实战(十一) 在Spring XML中配置AOP
2024-08-24 13:57:55
如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了。
1、Spring XML配置文件
解析参考:http://www.cnblogs.com/bigbigbigo/articles/8375530.html
2、AOP配置元素
aop配置元素 | 描述 |
<aop:advisor> | 定义aop通知器 |
<aop:after> | 定义aop后置通知(不管被通知的方法是否执行成功) |
<aop:after-returning> | 定义aop after-returning通知 |
<aop:after-throwing> | 定义aop after-throwing通知 |
<aop:around> | 定义aop环绕通知 |
<aop:aspect> | 定义切面 |
<aop:aspect-autoproxy> | 启动@AspectJ注解驱动的切面 |
<aop:before> | 定义aop前置通知 |
<aop:config> | 顶层的aop配置元素,大多数的<aop:*>元素必须包含在<aop:config>内 |
<aop:declare-parents> | 为被通知的 对象引入的额外的接口,并透明地实现 |
<aop:pointcut> | 定义切点 |
3、声明前置通知和后置通知
<aop:config>
<aop:aspect ref="audience">
<aop:before
pointcut="execution(** concert.Performance.perform(..))"
method="silenceCellPhone"/> <aop:before
pointcut="execution(** concert.Performance.perform(..))"
method="takeSeats"/> <aop:before
pointcut="execution(** concert.Performance.perform(..))"
method="applause"/> <aop:before
pointcut="execution(** concert.Performance.perform(..))"
method="demandRefund"/> </aop:aspect>
</aop:config>
使用<aop:pointcut>元素更简洁:
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/>
<aop:before
pointcut-ref="performance"
method="silenceCellPhones"/> <aop:before
pointcut-ref="performance"
method="takeSeats"/> <aop:before
pointcut-ref="performance"
method="applause"/> <aop:before
pointcut-ref="performance"
method="demandRefund"/> </aop:aspect>
</aop:config>
4、声明环绕通知
<aop:config> <!--声明环绕通知-->
<aop:aspect ref="audienceAround">
<aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/>
<aop:around pointcut-ref="performance" method="watchPerformance"/>
</aop:aspect> </aop:config>
5、为通知传递参数
假设方法perform(int numbers)有一个int型参数,而这个参数会传递到通知方法中。
因为在XML中,“&”符号会被解析为实体的开始,所以我们用“and”关键字代替“&&”。
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(** concert.Performance.perform(int)) and args(numbers)"/> <aop:before
pointcut-ref="performance"
method="demandRefund"/> </aop:aspect>
</aop:config>
6、为对象引入新的方法(通过切面引入新的功能)
之前通过使用@DeclareParents注解为被通知的对象引入新方法,但AOP因为不是AspecJ特有。
在XML中使用<aop:declare-parents>,可以实现相同的功能。
<aop:aspect>
<aop:declare-parents types-matching="concert.Performance+"
implement-interface="concert.Encoreable"
default-impl="concert.DefaultEncorable"/>
</aop:aspect>
这里使用default-impl属性来显示指定Encoreable的实现。
我们还可以使用delegate-ref属性来引用一个Spring bean作为引入的委托,这需要在Spring上下文中存在一个ID为encorableDelegate的bean:
<aop:aspect>
<aop:declare-parents types-matching="concert.Performance+"
implement-interface="concert.Encoreable"
default-ref="DefaultEncorable"/>
</aop:aspect>
使用default-impl,叫直接标识委托;
使用delegate-ref,叫引用一个bean作为引入的委托
最新文章
- Write Your software base on plugin(C/C++ ABI)
- Entity Framework学习 - 3.关联查询
- nginx模块开发(18)—日志分析
- andeoid学习笔记七
- 按钮控件Button
- linux下curl编程
- Android在子线程中更新UI(二)
- Spring入门学习(二)三种实例化bean的方法
- 基于Windows服务的聊天程序(一)
- Windows搭建golang开发平台
- php应用pack函数转unicode为utf8
- 3、MHC主要组织相容性复合体
- IQ调制原理
- win7重装系统笔记
- 【详解】JNI(Java Native Interface)(一)
- Codeforces Round #506 D. Concatenated Multiples题解
- [py]python中__new__作用
- BZOJ2209: [Jsoi2011]括号序列
- JDK中ClassLoader的分类以及ClassLoader间的层次关系
- Free GIS Software