转载地址: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.通过切面引入新方法

最新文章

  1. DB2数据库参数建议(Linux)
  2. css之absolute绝对定位(绝对定位特性)
  3. KVC , KVO , KVB
  4. HTML5和CSS3的能量究竟是怎样的?
  5. Insert后返回自动插入的生成的ID:select @@identity
  6. Windows系统基本概念
  7. android webview点击返回键回到上一个html
  8. 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
  9. 一个意想不到的Javascript内存泄漏
  10. 日志组件二:log4j2
  11. Filter、Interceptor、Aspect 区别及实现
  12. EffectiveC++ 第6章 继承与面向对象设计
  13. 160多个android开源码汇总
  14. oracle addm报告
  15. 【java】设计模式-单例设计模式
  16. java多线程系列14 设计模式 Master-Worker
  17. 关于微信小程序如何解决多层循环嵌套
  18. rabbitmq 消息的状态转换
  19. Jzzhu and Apples CodeForces - 449C (构造,数学)
  20. CentOS7 安装supervisor守护进程管理器

热门文章

  1. 在 vue 中用 transition 实现轮播效果
  2. STM32 PWM注意事项
  3. 由于SID连接不匹配,监听器拒绝连接。
  4. vtkTestHull将多个平面围成一个凸面体
  5. cocos2dx基础篇(5) 按钮
  6. python学习之模块-模块(三)
  7. http状态码 超详细
  8. 【miscellaneous】星光级超低照度摄像机技术分析
  9. 【VS开发】【图像处理】自动白平衡(AWB)算法---色温曲线
  10. magento下载地址