本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%AB%98%E7%BA%A7%E2%80%94%E2%80%94%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0%EF%BC%882%EF%BC%89Spring%20AOP%E4%B8%AD%E9%80%9A%E7%9F%A5%E5%99%A8%EF%BC%88Advisor%EF%BC%89%E4%B8%8E%E5%88%87%E9%9D%A2%EF%BC%88Aspect%EF%BC%89
之所以还未正式进入Spring AOP的源码,是因为我在阅读Spring AOP生成代理对象时遇到了一点小麻烦让我不得不暂时停止,转而理清有关Spring AOP中的两个概念性问题。
前面的博客里都没有提到过“通知器”这个概念,在《Spring实战》书中也只是简单地说明了在xml中
首先来讨论定义通知器相关的使用方法。 定义一个通知类,其中包含前置通知和后置通知,注意如果是使用
1 package com.demo; 2 3 import org.springframework.aop.AfterReturningAdvice; 4 import org.springframework.aop.MethodBeforeAdvice; 5 import org.springframework.stereotype.Component; 6 7 import java.lang.reflect.Method; 8 9 /** 10 * Created by Kevin on 2017/11/15. 11 */ 12 @Component("advisorTest") 13 public class AdvisorTest implements MethodBeforeAdvice, AfterReturningAdvice{ 14 15 /** 16 * 前置通知 17 * @param method 18 * @param args 19 * @param target 20 * @throws Throwable 21 */ 22 @Override 23 public void before(Method method, Object[] args, Object target) throws Throwable { 24 System.out.println("前置通知"); 25 } 26 27 /** 28 * 后置通知 29 * @param returnValue 30 * @param method 31 * @param args 32 * @param target 33 * @throws Throwable 34 */ 35 @Override 36 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { 37 System.out.println("后置通知"); 38 } 39 }
定义一个需要被代理的目标对象。
1 package com.demo; 2 3 import org.springframework.stereotype.Component; 4 5 /** 6 * 目标对象,需要被代理的类及方法 7 * Created by Kevin on 2017/11/15. 8 */ 9 @Component("testPoint") 10 public class TestPoint { 11 12 public void test() { 13 System.out.println("方法调用"); 14 } 15 }
我们要达到的目的就是在test方法调用前和调用后分别打印“前置通知”和“后置通知”。
applicationContext.xml中定义通知器如下:
1 xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 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"> 7 8 <context:component-scan base-package="com.demo"/> 9 10 <aop:config> 11 <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/> 12 <aop:advisor advice-ref="advisorTest" pointcut-ref="test"/> 13 aop:config> 14 15 beans>
最后的运行结果符合预期。那么问题来了,如果我们只想在定义的这个切点
接着来讨论定义切面相关的使用方法。 如果使用
1 package com.demo; 2 3 import org.springframework.stereotype.Component; 4 5 /** 6 * Created by Kevin on 2017/11/15. 7 */ 8 @Component("aspectTest") 9 public class AspectTest { 10 11 /** 12 * 前置通知 13 */ 14 public void doBefore() { 15 System.out.println("前置通知"); 16 } 17 18 /** 19 * 后置通知 20 */ 21 public void doAfter() { 22 System.out.println("后置通知"); 23 } 24 }
目标对象和上面的例子一致,紧接着是applicationContext.xml中切面的配置。
1 xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 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"> 7 8 <context:component-scan base-package="com.demo"/> 9 10 <aop:config> 11 <aop:aspect ref="aspectTest"> 12 <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/> 13 <aop:before method="doBefore" pointcut-ref="test"/> 14 <aop:after-returning method="doAfter" pointcut-ref="test"/> 15 aop:aspect> 16 aop:config> 17 beans>
可以看到我们通过
实际上可以这么说,通知器是一个特殊的切面。而在最开始那两篇博客中没有提到是因为那两个例子中使用的是AspectJ注解,而在AspectJ注解中并没有与此对应的概念。
在实际中用到的
这篇博客穿插在源码的其中是为了更好的理清Spring AOP中各种概念问题,缘由我在开头已经说过,接下来就正式开始Spring AOP源码的解读。
这是一个能给程序员加buff的公众号