转载地址: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中声明切面的更多相关文章

  1. Spring 在XML中声明切面/AOP

    在Spring的AOP配置命名空间中,我们能够找到声明式切面选择.看以下: <aop:config> <!-- AOP定义開始 --> <aop:pointcut/> ...

  2. 笔记11 在XML中声明切面(2)

    为通知传递参数 1.声明一个CompactDiscs接口.内部包含两个方法: show() 用于显示唱片的名字和艺术风格 playTrack(int number) 根据传入的磁道数播放相应磁道的音乐 ...

  3. 笔记10 在XML中声明切面(1)

    1.无注解的Audience package XMLconcert; public class Audience { public void silenceCellPhones() { System. ...

  4. Spring之AOP在XML中的配置方法

    AOP 即 Aspect Oriental Program 面向切面编程 先来一个栗子: <aop:config> <aop:pointcut id="loggerCutp ...

  5. Spring AOP之xml 配置实现

    首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...

  6. spring aop 使用xml方式的简单总结

    spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...

  7. Spring Boot 2.X(八):Spring AOP 实现简单的日志切面

    AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...

  8. J2EE进阶(五)Spring在web.xml中的配置

     J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...

  9. 使用Spring时web.xml中的配置

    使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...

随机推荐

  1. sql server 2014安装后用sa登录问题

    在使用的sql server的数据的情况下,安装数据过程,未指定使用sa的登录,只能使用windows的账户登录,那要怎么设置账户来使用sa账户登录账号呢? 首先先打开的是sql server man ...

  2. mybatis 动态SQL .1

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还要注意去掉 ...

  3. CTF—攻防练习之SMB私钥泄露

    攻击机:192.168.32.152 靶机 :192.168.32.155 打开靶机 nmap一下 我们看到了开放了 ssh,smb,mysql这些端口,还有一个大端口 对smb服务我们可以1.使用空 ...

  4. AWSome Day简介

    AWSome Day是什么? 它是一场为时一天.结合教育与技术新知的云计算技术免费研讨会.是面向所有开发人员.IT技术人员.或技术/业务领域决策者必备的基础云计算课程.AWS专业级讲师将在现场带领您从 ...

  5. VGA显示正圆

    接着上次的随笔,既然VGA时序已经实现了,那么就显示点东西看看吧. 想显示个圆,但是无从下手,参考了这篇文章:https://user.qzone.qq.com/1241003385/blog/154 ...

  6. MySQL改密

    一.未进入之前: 1.grep password /var/log/mysqld.log   得出默认密码2.更改密码    mysqladmin -uroot -p'd-tlbwIgP3e2' pa ...

  7. jQ的toggle() 方法

    语法:$(selector).toggle(speed,callback,switch) 实例: <script src="js/jquery.min.js">< ...

  8. Redis配置主从时报错“Could not connect to Redis at 192.168.0.50:6379: Connection refused not connected>”

    配置Redis主从时,修改完从节点配置文件,然后报错 [root@Rich七哥-0-50 redis]# /opt/redis/redis-cli -h 192.168.0.50 Could not ...

  9. zabbix监控java

    参考: 官网: https://www.zabbix.com/documentation/4.0/manual/config/items/itemtypes/jmx_monitoring

  10. Luogu P3195 [HNOI2008]玩具装箱

    题目 预处理\(C\)的前缀和\(sum\).设前\(i\)个物品的最小答案为\(f\). \(f_i=\max\limits_{j\in[1,i)}(f_j+(sum_i-sum_j-L)^2)\) ...