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>

* <dd>@date:2016年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.通过切面引入新方法

时间: 2024-08-26 06:28:47

Spring AOP 在XML中声明切面的相关文章

Spring_在XML中声明切面

人,最大的敌人是自己. AOP配置元素 在Spring的aop命名空间中,提供多个元素用来在XML中声明切面. 1)<aop:advisor>:定义AOP通知器 2)<aop:after>:定义AOP后置通知(不管被通知的方法是否执行成功) 3)<aop:after-returning>:定义AOP返回通知 4)<aop:after-throwing>:定义AOP异常通知 5)<aop:around>:定义AOP环绕通知 6)<aop:as

Spring 在XML中声明切面/AOP

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

Spring之AOP在XML中的配置方法

AOP 即 Aspect Oriental Program 面向切面编程 先来一个栗子: <aop:config> <aop:pointcut id="loggerCutpoint" expression= "execution(* com.how2java.service.ProductService.*(..)) "/> <aop:aspect id="logAspect" ref="loggerAsp

Spring_在XML中通过切面引入新的功能

没有不会做的事,只有不想做的事. 在Java配置中我们借助AspectJde @DeclareParents注解为被通知的方法引入新的方法,在XML中我们可以使用Spring aop命名空间的 <aop:declare-parents>元素. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/be

Spring(十九):Spring AOP(三):切面的优先级

背景: 1)指定切面优先级示例:有的时候需要对一个方法指定多个切面,而这多个切面有时又需要按照不同顺序执行,因此,切面执行优先级别指定功能就变得很实用. 2)重复使用切入点表达式:上一篇文章中,定义前置.后置.返回.异常通知的切入点表达式时,都使用了同一个:而且本章节新加入的验证切面ValidateAspect类,也使用同一个切入点表达式,怎么让他们重用呢? 指定切面优先级示例: 比如在算术计算器执行计算之前进行数据验证,验证完事之后才让执行日志输出. 新建spring aop项目参考:<Spr

Spring AOP配置-xml

基于xml的spring AOP配置主要有几个步骤: 1.创建切面类 编写自定义增强代码(如事务处理,日志等) 2.创建service 提供连接点 3.配置切面 在配置之前,先了解一些专业术语 连接点:被拦截的方法 切入点:拦截规则(符合规则被拦截的一类方法) 通知/增强:对拦截的方法添加自定义功能 切面:就是切面类,在其中自定义通知 编写切面类 //切面类 public class AspectClazz { public void save() { System.out.println("保

spring aop的xml配置详解

在Spring配置文件中,所以AOP相关定义必须放在<aop:config>标签下,该标签下可以有<aop:pointcut>.<aop:advisor>.<aop:aspect>标签,配置顺序不可变. <aop:pointcut>:用来定义切入点,该切入点可以重用: <aop:advisor>:用来定义只有一个通知和一个切入点的切面: <aop:aspect>:用来定义切面,该切面可以包含多个切入点和通知,而且标签内部的

spring在web.xml中的配置

在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载的容器中去,在web项目中,配置文件加载到web容器中进行解析,目前,spring提供了两种加载器,以供web容器的加载:一种是ContextLoaderListener,另一种是ContextLoaderServlet.这两种在功能上完全相同,只是一种是基于Servlet2.3版本中新引入的Listener接口实现,而另一种是基于Servlet接口实现,以下是这两种加载器在w

SpringMVC: web.xml中声明DispatcherServlet时一定要添加load-on-startup标签

游历SpringMVC源码后发现,在web.xml中注册的ContextLoaderListener监听器只是初始化了一个根上下文,仅仅完成了组件扫描和与容器初始化相关的一些工作,并没有探测到具体每个URL应当map到哪个Controller, 哪个方法上.而剩一下的这些复杂工作都是由DispatcherServet来完成的,即应用服务器加载DispatcherServlet调用init()方法时才能触发这项工作.所以,如果在web.xml中配置DispatcherServlet时不设置 <lo