[Spring实战系列](17)编写切点与声明切面



切点用于准确定位应该在什么地方应用切面的通知。切点和通知是切面的最基本元素。

在Spring AOP中,需要使用AspectJ的切点表达式语言来定义切点。关于Spring AOP的AspectJ切点,最重要的一点是Spring仅支持AspectJ切点指示器的一个子集。
类型 说明
arg() 限制连接点匹配参数为指定类型的执行方法
@args() 限制连接点匹配参数由指定注解标注的执行方法
execution() 用于匹配的是连接点的执行方法
this() 限制连接点匹配AOP代理的Bean引用为指定类型的类。
target() 限制连接点匹配目标对象为指定类型的类。
@target() 限制连接点匹配特定的执行对象,这些对象对应的类具备指定类型的注解。
within() 限制连接点匹配指定的类型。
@within() 限制连接点匹配指定注解所标注的类型(当使用Spring AOP时,方法定义再由指定的注解所标注的类里)。
@annotation 限制匹配带有指定注解连接点。

在Spring中尝试使用AspectJ其他指示器时,将会抛出IllegalArgumentException异常。

注意:

只有execution指示器是唯一的执行匹配,而其他的指示器都是用于限制匹配的。这说明execution指示器是我们在编写切点定义时最主要的指示器。在此基础上,我们使用其他指示器来限制所匹配的切点。
1. 编写切点
如下图所示的切点表达式表示当Singer的perform()方法执行时会触发通知。我们使用execution()指示器选择Singer的perform()方法。 方法表达式以*号开始,标示了我们不关心返回值的类型。然后,我们 指定了全限定类名和方法名。对于 方法参数列表,我们使用(..)标示切点选择任意的perform()方法,无论该方法的参数是什么
QQ截图20160212201528.png

除此之外,我们还可以对上面的匹配进行限制,可以使用 within()指示器来限制匹配
QQ截图20160212202634.png

我们使用&&操作符把execution()和within()指示器连接在一起形成and关系(切点必须匹配所有的指示器)。
2. 在XML中声明切面
Spring的AOP配置元素简化了基于POJO切面的声明:
类型 说明
<aop:advisor> 定义AOP通知器。
<aop:after> 定义AOP后置通知(不管被通知的方法是否执行成功)
<aop:after-returning> 定义AOP after-returning通知。
<aop:after-throwing> 定义 AOP after-throwing 通知。
<aop:around> 定义 AOP 环绕通知。
<aop:aspect> 定义切面。
<aop:aspectj-autoproxy> 启用@AspectJ注解驱动的切面。
<aop:before> 定义 AOP前置通知。
<aop:config> 顶层的AOP配置元素。大多数的<aop:*>元素必须包含在<aop:config>元素内。
<aop:declare-parents> 为被通知的对象引入额外的接口,并透明的实现。
<aop:pointcut> 定义切点。

为了阐述Spring AOP,我们创建一个观众类(Audience)类:
   
   
package com.sjf.bean;
/**
* 观众类
* @author sjf0115
*
*/
public class Audience {
public void takeSeats(){
System.out.println("the audience is taking their seats...");
}
public void applaud(){
System.out.println("very good, clap clap clap...");
}
public void demandRefund(){
System.out.println("very bad, We want our money back...");
}
}

Audience类并没有任何特别之处,她就是一个有几个方法的简单Java类。我们可以像其他类一样,利用XML把它注册为Spring应用上下文中的一个Bean:
   
   
<bean id = "audience" class = "com.sjf.bean.Audience">
</bean>
我们需要Spring AOP就能把它成为一个切面。
2.1 前置声明和后置声明

   
   
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id = "singer" class = "com.sjf.bean.Singer">
</bean>
<bean id = "audience" class = "com.sjf.bean.Audience">
</bean>
<aop:config proxy-target-class="true">
<!-- 声明定义一个切面 -->
<aop:aspect ref = "audience">
<!-- 表演之前 -->
<aop:before method="takeSeats" pointcut="execution(* com.sjf.bean.Singer.perform(..))"/>
<!-- 表演之后 -->
<aop:after-returning method="applaud" pointcut="execution(* com.sjf.bean.Singer.perform(..))"/>
<!-- 表演失败之后 -->
<aop:after-throwing method="demandRefund" pointcut="execution(* com.sjf.bean.Singer.perform(..))"/>
</aop:aspect>
</aop:config>
</beans>
大多数的AOP配置元素必须在<aop:config>元素的上下文内使用。这条规则有几种例外场景,但是把Bean声明为一个切面时,我们总是从<aop:config>元素开始配置。

在<aop:config>元素内,我们可以声明一个或者多个通知器,切面或者切点。上述例子中,我们使用 <aop:aspect>元素声明了一个简单的切面。ref元素引用了一个Bean(Audience),该Bean实现了切面的功能。 ref元素应用的Bean提供了在切面上通知所调用的方法。

该切面应用了3个不同的通知。 <aop:before>元素定义了匹配切点的方法执行之前调用前置通知方法,audience Bean 的takeSeats()方法。 <aop:after-returning>元素定义了一个返回后(after-returning)通知,在切点所匹配的方法调用之后在执行applaud()方法。 <aop:after-throwing>元素定义了抛出异常后通知,如果所有匹配的方法执行时抛出任何异常,都将调用demandRefund()方法。

下面展示了通知逻辑如何嵌入到业务逻辑中:
QQ截图20160211221047.png

在所有的通知元素中, pointcut属性定义了通知所应用的切点。pointcut属性的值是使用AspectJ切点表达式语法所定义的切点。

你或许注意到所有通知元素中的pointcut属性的值都是一样的,这是因为所有的通知都是应用到相同的切点上。这似乎违反了DRY(不要重复你自己)原则。我们做一下修改,可以 使用<aop:pointcut>元素定义一个命名切点
   
   
<aop:config proxy-target-class="true">
<!-- 声明定义一个切面 -->
<aop:aspect ref = "audience">
<aop:pointcut id="singerPerfom" expression="execution(* com.sjf.bean.Singer.perform(..))" />
<!-- 表演之前 -->
<aop:before method="takeSeats" pointcut-ref="singerPerfom"/>
<!-- 表演之后 -->
<aop:after-returning method="applaud" pointcut-ref="singerPerfom"/>
<!-- 表演失败之后 -->
<aop:after-throwing method="demandRefund" pointcut-ref="singerPerfom"/>
</aop:aspect>
</aop:config>
<aop:pointcut>元素定义了一个id为singerPerfom的切点,同时修改所有的通知元素, 用pointcut-ref属性来引用这个命名切点
2.2 声明环绕通知
如果不适用成员变量存储信息,那么在前置通知和后置通知之间共享信息是非常麻烦的。我们希望实现这样一个功能:希望观众一直关注演出,并报告演出的演出时长。使用前置通知和后置通知实现该功能的唯一方式是:在前置通知中记录开始时间,并在某个后置通知中报告演出的时长。但这样的话,我们必须在一个成员变量中保存开始时间。因此我们可以使用环绕通知,只需在一个方法中实现即可。
   
   
public void PerformTime(ProceedingJoinPoint joinPoint){
// 演出之前
System.out.println("the audience is taking their seats...");
try {
long start = System.currentTimeMillis();
// 执行演出操作
joinPoint.proceed();
long end = System.currentTimeMillis();
// 演出成功
System.out.println("very good, clap clap clap...");
System.out.println("该演出共需要 "+(end - start) + " milliseconds");
} catch (Throwable e) {
// 演出失败
System.out.println("very bad, We want our money back...");
e.printStackTrace();
}
}

对于这个新的通知方法,我们会注意到它使用了ProceedingJoinPoint作为方法的入参。这个对象非常重要,因为它能 让我们在通知里调用被通知 的方法。如果希望把控制转给被通知的方法时,我们可以调用 ProceedingJoinPoint的proceed()方法。如果忘记调用proceed()方法,通知将会阻止被通知方法的调用。我们还可以在通知里多次调用被通知方法,这样做的一个目的是实现重试逻辑,在被通知的方法执行失败时反复重试。

PerformTime()方法包含了之前3个通知方法的所有逻辑,并且该方法还会负责自身的异常处理。声明环绕通知与声明其他类型的通知并没有太大的区别,只需要<aop:around>元素
   
   
<aop:config proxy-target-class="true">
<!-- 声明定义一个切面 -->
<aop:aspect ref = "audience">
<aop:pointcut id="singerPerfom" expression="execution(* com.sjf.bean.Singer.perform(..))" />
<!-- 声明环绕通知 -->
<aop:around method="performTime" pointcut-ref="singerPerfom"/>
</aop:aspect>
</aop:config>

运行结果:

the audience is taking their seats...
正在上演个人演唱会...
very good, clap clap clap...
该演出共需要 37 milliseconds  

像其他通知的XML一样,<aop:around>指定了一个切点和一个通知方法的名字。
2.3 为通知传递参数
到目前为止,我们的切面很简单,没有任何的参数。但是有时候通知并不是仅仅是对方法进行简单包装,还需要校验传递给方法的参数值,这时候为通知传递参数就非常有用了。

下面是歌手的实体类:
   
   
package com.sjf.bean;
/**
* 歌手实体类
* @author sjf0115
*
*/
public class Singer {
 
public void perform(String songName) {
System.out.println("正在上演个人演唱会,演唱曲目为 " + songName);
}
}
在这我们提供了一个Organizers(主办方)实体类,在歌手演唱之前截获歌手演唱的曲目,然后通知给大家:
   
   
package com.sjf.bean;
/**
* 主办方实体类
* @author sjf0115
*
*/
public class Organizers {
public void BeforeSong(String songName){
System.out.println("演唱会马上就开始了,演唱歌曲为 " + songName);
}
public void AfterSong(){
System.out.println("演唱曲目结束,谢谢大家...");
}
}

我们像以前一样使用<aop:aspect>,<aop:before>和<aop:after>元素。但是这次我们 通过配置实现将被通知方法的参数传递给通知
   
   
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id = "singer" class = "com.sjf.bean.Singer">
</bean>
<bean id = "Organizers" class = "com.sjf.bean.Organizers">
</bean>
<aop:config>
<!-- 声明定义一个切面 -->
<aop:aspect ref = "Organizers">
<aop:pointcut id="singerPerform" expression="execution(* com.sjf.bean.Singer.perform(String)) and args(song)" />
<aop:pointcut id="singerPerform2" expression="execution(* com.sjf.bean.Singer.perform(..))" />
<aop:before method="BeforeSong" pointcut-ref="singerPerform" arg-names="song"/>
<aop:after-returning method="AfterSong" pointcut-ref="singerPerform2"/>
</aop:aspect>
</aop:config>
</beans>

关键之处在于切点定义和<aop:before>的arg-names属性。切点标示了Singer的perform()方法,指定了String参数。然后在args参数中标示了song作为参数。同样, <aop:before>元素引用了切点中 song参数,标示该参数必须传递给Organizers的BeforeSong()方法

QQ截图20160212195008.png

运行结果:

演唱会马上就开始了,演唱歌曲为 你是我的眼泪
正在上演个人演唱会,演唱曲目为 你是我的眼泪
演唱曲目结束,谢谢大家...  


来源于:《Spring实战》


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值