转载地址: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. nodejs之express中间件路由使用

    1.express 中间件使用 /* * 中间件:就是匹配路由之前和匹配路由之后做的一系列操作 */ var express = require('express'); var app = new e ...

  2. 为解决Thymeleaf数字格式化问题而想到的几种方案

    背景: spring后端输出double类型数据,前端使用thymeleaf框架,格式化double数据类型,由于需要显示原值(比如原来录入5,而不能显示5.00),因此需要存储数值(存储值为deci ...

  3. 【转】Eureka集群

    Eureka作为SpringCloud的服务发现与注册中心,在整个的微服务体系中,处于核心位置.单一的eureka服务,显然不能满足高可用的实际生产环境,这就要求我们配置一个能够应对各种突发情况,具有 ...

  4. Golang基础(5):Go语言反射规则

    Go语言反射规则 - The Laws of Reflection 转:http://my.oschina.net/qbit/blog/213720 原文地址:http://blog.golang.o ...

  5. LeetCode.976-周长最大的三角形(Largest Perimeter Triangle)

    这是悦乐书的第368次更新,第396篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第230题(顺位题号是976).给定正长度的数组A,返回具有非零区域的三角形的最大周长, ...

  6. 【miscellaneous】网络摄像机

    自20世纪90年代初期网络摄像机开始诞生,产业已历经20余年的演变. "IP大时代"的口号在安防领域已响彻已久,但也是自2015年至今才开使有了真正的底气.当全面超越模拟已尘埃落定 ...

  7. [ASP.NET] 解决点击控件下载文件没有响应的问题

    下载文件的方法是使用http响应输出流来实现的,使用到了response.write() 导致下载文件时点击控件出错,没有响应,也获取不了文件 是因为在母版页使用了updatepanel,因此回传时发 ...

  8. python 并发编程目录

    操作系统介绍 操作系统发展史 进程理论 多进程 多线程 协程 io模型

  9. HDU 1176 免费馅饼 (动态规划、另类数塔)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. poj2407(欧拉函数模板题)

    题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...