Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
之所以还未正式进入Spring AOP的源码,是因为我在阅读Spring AOP生成代理对象时遇到了一点小麻烦让我不得不暂时停止,转而理清有关Spring AOP中的两个概念性问题。
前面的博客里都没有提到过“通知器”这个概念,在《Spring实战》书中也只是简单地说明了在xml中<aop:advisor>用于定义一个通知器,此后便没再说明,而是使用<aop:aspect>定义一个切面。而在《Spring技术内幕》中有关Spring AOP章节中则是介绍了AOP中三个概念:通知、切点、通知器。在这时,我对“通知器”产生了很大的疑惑,查阅了相关资料并没有满意的答案,于是决定自己一探究竟。
首先来讨论定义通知器相关的使用方法。 定义一个通知类,其中包含前置通知和后置通知,注意如果是使用<aop:advisor>定义通知器的方式实现AOP则需要通知类实现Advice接口,前置通知方法对应的是MethodBeforeAdvice,后置通知方法对应的是AfterReturningAdvice。
package com.demo; import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component; import java.lang.reflect.Method; /**
* Created by Kevin on 2017/11/15.
*/
@Component("advisorTest")
public class AdvisorTest implements MethodBeforeAdvice, AfterReturningAdvice{ /**
* 前置通知
* @param method
* @param args
* @param target
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知");
} /**
* 后置通知
* @param returnValue
* @param method
* @param args
* @param target
* @throws Throwable
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置通知");
}
}
定义一个需要被代理的目标对象。
package com.demo; import org.springframework.stereotype.Component; /**
* 目标对象,需要被代理的类及方法
* Created by Kevin on 2017/11/15.
*/
@Component("testPoint")
public class TestPoint { public void test() {
System.out.println("方法调用");
}
}
我们要达到的目的就是在test方法调用前和调用后分别打印“前置通知”和“后置通知”。
applicationContext.xml中定义通知器如下:
<?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/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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.demo"/> <aop:config>
<aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>
<aop:advisor advice-ref="advisorTest" pointcut-ref="test"/>
</aop:config> </beans>
最后的运行结果符合预期。那么问题来了,如果我们只想在定义的这个切点 <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>里只配置前置通知,这个时候怎么办呢?答案是,通过以上方式是不可以的。也就是说如果通过定义Advisor的方式,在有的地方比较局限,狭隘来讲通过定义Advisor通知器的方式,只能定义只有一个通知和一个切入点的切面。当然一个通知不准确,因为上面可以看到只要实现不同的通知接口即可代理,但如果实现了多个通知接口,而只想使用一个时就不可以了。通知器是一个特殊的切面。
接着来讨论定义切面相关的使用方法。 如果使用<aop:aspect>定义切面的方式,通知类是可以不用实现任何通知接口的,这是很大一个便利。同样要实现上面例子的功能,定义一个通知类,包括前置通知和后置通知。
package com.demo; import org.springframework.stereotype.Component; /**
* Created by Kevin on 2017/11/15.
*/
@Component("aspectTest")
public class AspectTest { /**
* 前置通知
*/
public void doBefore() {
System.out.println("前置通知");
} /**
* 后置通知
*/
public void doAfter() {
System.out.println("后置通知");
}
}
目标对象和上面的例子一致,紧接着是applicationContext.xml中切面的配置。
<?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/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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.demo"/> <aop:config>
<aop:aspect ref="aspectTest">
<aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>
<aop:before method="doBefore" pointcut-ref="test"/>
<aop:after-returning method="doAfter" pointcut-ref="test"/>
</aop:aspect>
</aop:config>
</beans>
可以看到我们通过<aop:aspect>定义了一个切面,如果只需要前置通知,则只定义<aop:before>就可以了,这和<aop:advisor>是很大的不同,由此可知通过<aop:aspect>定义切面的方式可以在其中灵活地定义通知,而不必像通知器那样约束。
实际上可以这么说,通知器是一个特殊的切面。而在最开始那两篇博客中没有提到是因为那两个例子中使用的是AspectJ注解,而在AspectJ注解中并没有与此对应的概念。
在实际中用到的<aop:advisor>场景最多的莫过于在Spring中配置事务。除此之外,很少用到,也不建议使用。因为最大的一个问题就是定义通知时需要实现通知接口,这违背了一点Spring“非侵入式”编程的初衷。
这篇博客穿插在源码的其中是为了更好的理清Spring AOP中各种概念问题,缘由我在开头已经说过,接下来就正式开始Spring AOP源码的解读。
这是一个能给程序员加buff的公众号

Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)的更多相关文章
- Spring AOP高级——源码实现(3)AopProxy代理对象之JDK动态代理的创建过程
spring-aop-4.3.7.RELEASE 在<Spring AOP高级——源码实现(1)动态代理技术>中介绍了两种动态代理技术,当然在Spring AOP中代理对象的生成也是运用 ...
- Spring AOP高级——源码实现(1)动态代理技术
在正式进入Spring AOP的源码实现前,我们需要准备一定的基础也就是面向切面编程的核心——动态代理. 动态代理实际上也是一种结构型的设计模式,JDK中已经为我们准备好了这种设计模式,不过这种JDK ...
- Spring Boot 启动源码解析结合Spring Bean生命周期分析
转载请注明出处: 1.SpringBoot 源码执行流程图 2. 创建SpringApplication 应用,在构造函数中推断启动应用类型,并进行spring boot自动装配 public sta ...
- Feign 系列(05)Spring Cloud OpenFeign 源码解析
Feign 系列(05)Spring Cloud OpenFeign 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/ ...
- 专治不会看源码的毛病--spring源码解析AOP篇
昨天有个大牛说我啰嗦,眼光比较细碎,看不到重点.太他爷爷的有道理了!要说看人品,还是女孩子强一些.原来记得看到一个男孩子的抱怨,说怎么两人刚刚开始在一起,女孩子在心里就已经和他过完了一辈子.哥哥们,不 ...
- Spring源码剖析7:AOP实现原理详解
前言 前面写了六篇文章详细地分析了Spring Bean加载流程,这部分完了之后就要进入一个比较困难的部分了,就是AOP的实现原理分析.为了探究AOP实现原理,首先定义几个类,一个Dao接口: pub ...
- 九、Spring之BeanFactory源码分析(一)
Spring之BeanFactory源码分析(一) 注意:该随笔内容完全引自https://blog.csdn.net/u014634338/article/details/82865644,写的 ...
- Spring IOC 容器源码分析 - 余下的初始化工作
1. 简介 本篇文章是"Spring IOC 容器源码分析"系列文章的最后一篇文章,本篇文章所分析的对象是 initializeBean 方法,该方法用于对已完成属性填充的 bea ...
- Spring IOC 容器源码分析 - 填充属性到 bean 原始对象
1. 简介 本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的.我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反 ...
随机推荐
- BZOJ-5055-膜法师(离散化+树状数组)
Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...
- Mysql安装后打开MySQL Command Line Client闪退解决方法
1.开始菜单下;Mysql--->mysql server 5.6-->mysql command line Client ---右击,选择属性 2.在属性下查看目标位置: 3.将安装目录 ...
- firewalld 操作实践
1.firewalld 从名称上看,模仿的是硬件防火墙的概念,zone. 所有的接口都必须属于某个zone . 在zone内配置规则. 2. 常用的方法是 增加对一个tcp或者udp端口号的允许通过 ...
- 验证代理IP
##author:wuhao#import urllib.requestfrom http import cookiejar import xlrd import threading #有效的代理,可 ...
- iOS自动化环境搭建——macaca
macaca-java for ios 自动化环境搭建 基础原理解析:https://testerhome.com/topics/6608 一.环境搭建 1.安装eclipse; -----Java开 ...
- Java中public,protected,default,private的访问权限问题(简明扼要)
import packa.*;//导入了packa包中所有的类.(不包括包中的子包)一般不会用,用哪个导入哪个. 导包的原则:用到哪个类,就导入哪个类.所有字母都小写. 权限列表: public ...
- (转)十分钟入门pandas
本文是对pandas官方网站上<10 Minutes to pandas>的一个简单的翻译,原文在这里.这篇文章是对pandas的一个简单的介绍,详细的介绍请参考:Cookbook . 习 ...
- Traefik实现Kubernetes集群服务外部https访问
转载请注明出处:http://www.cnblogs.com/wayneiscoming/p/7707942.html traefik 是一个前端http反向代理服务器以及负载均衡器,支持多种微服务后 ...
- shell编程/字库裁剪(2)——编程过程
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7704085.html 作者:窗户 Q ...
- 5分钟看懂svg path 路径的所有命令(更有API解释、有图、有图文对比解析)
友情提示:更多详情.每个命令的例子.参数变化对比图文详解,欢迎关注九十七度的博客:SVG<Path>命令详解 M = moveto M x y 移动到指定坐标,xy分别为x轴和y轴的坐标点 ...