Spring AOP 在XML中声明切面
转载地址:http://www.jianshu.com/p/43a0bc21805f
在XML中将一个Java类配置成一个切面:
|
AOP元素 |
用途 |
|
<aop:advisor> |
定义AOP通知器 |
|
<aop:after> |
定义一个后置通知(不管目标方法是否执行成功) |
|
<aop:after-returning> |
定义AOP返回通知 |
|
<aop:after-throwing> |
定义AOP异常通知 |
|
<aop:around> |
定义环绕通知 |
|
<aop:aspect> |
定义一个切面 |
|
<aop:aspectj-autoproxy> |
启动@AspectJ注解驱动的切面 |
|
<aop:before> |
定义一个AOP前置通知 |
|
<aop:config> |
顶层AOP配置元素。大多数的<aop:*>元素都必须包含在<aop:config>元素内 |
|
<aop:declare-parents> |
以透明的方式为被通知的对象引入额外的接口 |
|
<aop:pointcut> |
定义一个切点 |
我们之前已经看过了<aop:adpectj-autoproxy>元素,他能够自动代理AspectJ注解的通知类。aop的其他元素可以让我们直接在XML中配置切面,而不使用注解,下面我们定义一个不使用任何注解的Audience类:
package com.spring.aop.service.aop;
/**
* <dl>
* <dd>Description:观看演出的切面</dd>
* <dd>Company: 黑科技</dd>
年9月3日下午9:58:09</dd>
* <dd>@author:Kong</dd>
* </dl>
*/
@Aspect
public class Audience {
/**
* 目标方法执行之前调用
*/
public void silenceCellPhone() {
System.out.println("Silencing cell phones");
}
/**
* 目标方法执行之前调用
*/
public void takeSeats() {
System.out.println("Taking seats");
}
/**
* 目标方法执行完后调用
*/
public void applause() {
System.out.println("CLAP CLAP CLAP");
}
/**
* 目标方法发生异常时调用
*/
public void demandRefund() {
System.out.println("Demanding a refund");
}
}
现在看来Audience类和普通的Java类没有任何的区别,但是我们只需要在Xml中稍作配置,他就可以成为一个切面。
1.声明前置和后置通知
<aop:config>
<aop:aspect ref="audience">
<aop:before pointcut="execution(** com.spring.aop.service.Perfomance.perform(..)"
method="silenceCellPhone"/>
<aop:before pointcut="execution(** com.spring.aop.service.Perfomance.perform(..)"
method="takeSeats"/>
<aop:after-returning pointcut="execution(** com.spring.aop.service.Perfomance.perform(..)"
method="applause"/>
<aop:after-throwing pointcut="execution(** com.spring.aop.service.Perfomance.perform(..)"
method="demandRefund"/>
</aop:aspect>
</aop:config>
聪明的小伙伴一定又发现了,相同的切点我们写了四次,这是不科学的,强大的Spring不会允许有这样的Bug出现,你猜对了,可以使用<aop:pointcut>元素定义一个公共的切点,而且这个切点还可以定义在切面类的外边,供其他的切面使用:
<aop:config>
<aop:pointcut id="performance"
expression="execution(** com.spring.aop.service.Perfomance.perform(..)" />
<aop:aspect ref="audience">
<aop:before pointcut-ref="performance" method="silenceCellPhone"/>
<aop:before pointcut-ref="performance" method="takeSeats"/>
<aop:after-returning pointcut-ref="performance" method="applause"/>
<aop:after-throwing pointcut-ref="performance"method="demandRefund"/>
</aop:aspect>
</aop:config>
2.在Xml中配置环绕通知
3.为通知传递参数
4.通过切面引入新方法
Spring AOP 在XML中声明切面的更多相关文章
- Spring 在XML中声明切面/AOP
在Spring的AOP配置命名空间中,我们能够找到声明式切面选择.看以下: <aop:config> <!-- AOP定义開始 --> <aop:pointcut/> ...
- 笔记11 在XML中声明切面(2)
为通知传递参数 1.声明一个CompactDiscs接口.内部包含两个方法: show() 用于显示唱片的名字和艺术风格 playTrack(int number) 根据传入的磁道数播放相应磁道的音乐 ...
- 笔记10 在XML中声明切面(1)
1.无注解的Audience package XMLconcert; public class Audience { public void silenceCellPhones() { System. ...
- Spring之AOP在XML中的配置方法
AOP 即 Aspect Oriental Program 面向切面编程 先来一个栗子: <aop:config> <aop:pointcut id="loggerCutp ...
- Spring AOP之xml 配置实现
首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...
- spring aop 使用xml方式的简单总结
spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...
- Spring Boot 2.X(八):Spring AOP 实现简单的日志切面
AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- 使用Spring时web.xml中的配置
使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...
随机推荐
- Java String.Format() 方法及参数说明
转自:https://blueram.iteye.com/blog/441683 JDK1.5中,String类新增了一个很有用的静态方法String.format():format(Locale l ...
- 【flask】使用pipenv管理依赖环境
[前堤] 已经安装了pipenv环境,并且项目目录下存在pipfile 如果要在另一个开发环境做开发,则将代码和Pipfile复制过去,运行以下命令: pipenv install --dev 是否要 ...
- ControlTemplate in WPF —— Button
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- 详解git pull和git fetch的区别
前言 在我们使用git的时候用的更新代码是git fetch,git pull这两条指令.但是有没有小伙伴去思考过这两者的区别呢?有经验的人总是说最好用git fetch+git merge,不建议用 ...
- 90子集II
题目:给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).说明:解集不能包含重复的子集. 输入:[1,2,2] 输出:[[2],[1],[1,2,2],[2,2],[1 ...
- c++ 函数后面有个 const
非静态成员函数后面加const,类似如下函数: class testClass{ public: void testClass() const{ /*...*/} private: /*...*/ } ...
- docker安装详解
系统要求 64位操作系统 Linux kernel大于等于3.10 CentOS 7以及以上版本 yum安装 移除旧版本docker信息 # yum -y remove docker docker-c ...
- 微软宣布加入 OpenJDK,打不过就改变 Java 未来!
近日,微软的 Bruno Borges 在 OpenJDK 邮件列表中发布了一条消息,内容包含接下来发生的事情以及微软如何开始将其团队整合到 OpenJDK 社区. 在邮件中,Bruno Borges ...
- postgresql 用 like 可以 复制结构包括主键约束
create tabletablename ( like tablename INCLUDING INDEXES INCLUDING COMMENTS); PostgreSQL 动态表复制(CREAT ...
- Makefile中include、-include、sinclude
include.-include.sinclude使用 在 Makefile 使用 include 关键字可以把别的 Makefile 包含进来,这很像 C 语言的#include,被包含的文件会原模 ...