Spring实战(十一) 在Spring XML中配置AOP
如果你没有源码,不能为通知类添加注解,又不想将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作为引入的委托
Spring实战(十一) 在Spring XML中配置AOP的更多相关文章
- Spring学习记录(十三)---基于xml文件配置AOP
上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- 源码跟读,Spring是如何解析和加载xml中配置的beans
Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086c ...
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
- spring 和springmvc 在 web.xml中的配置
(1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...
- 10 Spring框架--基于注解和xml的配置的应用案例
1.项目结构 2.基于xml配置的项目 <1>账户的业务层接口及其实现类 IAccountService.java package lucky.service; import lucky. ...
- struts2 在 Action 或 Interceptor 中获取 web.xml 中配置的 <context-param> 参数 (这是我的第一篇博文,哈哈。)
最近为了改一个问题,想加一个控制开关,就在web.xml 中配置了一个 <context-param> 参数,并在 Action 或 Interceptor 中获取参数值. 1.在 web ...
- 在web.xml中配置监听器来控制ioc容器生命周期
5.整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因: 1.配置的组件太多,需保障单实例 2.项目停止后,ioc容器也需要关掉,降低对内存资源的占用. 项目启动创建容器,项目停止销 ...
- 在web.xml中配置error-page
在web.xml中配置error-page 在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下: 一. 通过错误码来配置error ...
随机推荐
- Ubuntu 16.04 一键安装P4开发环境记录
写在最前 P4开发环境安装可采用陈翔同学的一键安装脚本:p4Installer p4c-bm是P4-14的编译器,p4c是现在主流P4-16的编译器,bmv2是支持P4运行的软件交换机 系统环境 在安 ...
- 3.rabbitmq--发布订阅模式
rabbitmq-----发布订阅模式 模型组成 一个消费者Producer,一个交换机Exchange,多个消息队列Queue,多个消费者Consumer 一个生产者,多个消费者,每一个消费者都有 ...
- 15.反转链表 Java
题目描述 输入一个链表,反转链表后,输出新链表的表头. 思路 本题的关键就是在于对next域的赋值,同时对下一个节点进行保存,然后对把下一个节点赋给新的节点,这样依次循环完所有的节点.每次使新插入的节 ...
- Netfilter 之 iptable_mangle
初始化 iptable_mangle_table_init函数通过调用ipt_register_table完成mangle表注册和钩子函数注册的功能:该流程与iptable_filter的函数调用的函 ...
- 使用 usb 调试的时候,连接上电脑没反应
使用 usb 调试的时候,连接上电脑没反应 原因: 手机上没有信任本计算机的授权,请在手机上信任该授权 解决方法: 原因就是手机上会有一个弹话框,让我们信任该计算机,我们才可以进行 usb调试 我们的 ...
- DockerFile 简单使用
Dockerfile 是用来构建 Docker 镜像的构建文件,是由一系列命令和参数构成的脚本. 以 CentOS 为例(https://hub.docker.com/_/centos): FROM ...
- 前端知识点回顾之重点篇——AJAX
Ajax(Asynchronous JavaScript and XML) 这种技术就是无须刷新页面即可从服务器中取得数据,但不一定是XML数据.在原生方法上,Ajax技术的核心是XMLHttpReq ...
- python git 基础操作
模块安装 pip install gitpython 基本用法 1. 初始化 from git import Repo Repo.init('/data/test2') # 创建一个git文件夹 # ...
- UML绘图工具
画UML图与写文章差不多,都是把自己的思想描述给别人看,关键在于思路和条理,图好看与否就是看你的字是否规范,至于工具,就像你用什么笔,不算非常重要. 目前市场上常见的建模工具有StarUML,IBM ...
- 两个input之间有空隙,处理方法
修改css,给前边一个input添加一个左浮动. <input id="day" type="button" value="日" ...