博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring AOP 在XML中声明切面
阅读量:4560 次
发布时间:2019-06-08

本文共 2310 字,大约阅读时间需要 7 分钟。

 

转载地址:

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>@date201693下午9:58:09</dd>

* <dd>@authorKong</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.通过切面引入新方法

转载于:https://www.cnblogs.com/xiaolang8762400/p/7022262.html

你可能感兴趣的文章
git 初级
查看>>
[hdu4347]The Closest M Points(平衡树式kdtree)
查看>>
[hdu2874]Connections between cities(LCA+并查集)
查看>>
web端功能测试总结(二)
查看>>
Thymeleaf-模板引擎
查看>>
去重算法
查看>>
POJ 3298 Antimonotonicity (思维)
查看>>
c#获取硬件信息
查看>>
【转】寻找第K大的数的方法总结
查看>>
php redis安装
查看>>
SQL 语句 - Select(8): 分组条件
查看>>
如何把UIView转成UIImage,解决模糊失真问题
查看>>
ISAPI_Rewrite应用技巧与方法
查看>>
Linux企业级开发技术(5)——libevent企业级开发之简介
查看>>
Python Appium 滑动、点击等操作
查看>>
angular2-7中的变化监测
查看>>
iOS APP上架及版本迭代
查看>>
Spring Boot—03REST请求
查看>>
Memcache 和 Redis
查看>>
shell中script和scriptreplay使用
查看>>