本文节选自《Spring 5核心原理》
前面我们已经完成了Spring IoC、DI、MVC三大核心模块的功能,并保证了功能可用。接下来要完成Spring的另一个核心模块—AOP,这也是最难的部分。
1 基础配置
首先,在application.properties中增加如下自定义配置,作为Spring AOP的基础配置:
#多切面配置可以在key前面加前缀#例如 aspect.logAspect.#切面表达式#pointCut=public .* com.tom.spring.demo.service..*Service..*(.*)#切面类#aspectClass=com.tom.spring.demo.aspect.LogAspect#切面前置通知#aspectBefore=before#切面后置通知#aspectAfter=after#切面异常通知#aspectAfterThrow=afterThrowing#切面异常类型#aspectAfterThrowingName=java.lang.Exception
为了加强理解,我们对比一下Spring AOP的原生配置:
<bean id="xmlAspect" class="com.gupaoedu.aop.aspect.XmlAspect"></bean><!-- AOP配置 --><aop:config><!-- 声明一个切面,并注入切面Bean,相当于@Aspect --><aop:aspect ref="xmlAspect"><!-- 配置一个切入点,相当于@Pointcut --><aop:pointcut expression="execution(* com.gupaoedu.aop.service..*(..))" id="simplePointcut"/><!-- 配置通知,相当于@Before、@After、@AfterReturn、@Around、@AfterThrowing --><aop:before pointcut-ref="simplePointcut" method="before"/><aop:after pointcut-ref="simplePointcut" method="after"/><aop:after-returning pointcut-ref="simplePointcut" method="afterReturn"/><aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex"/></aop:aspect></aop:config>
为了方便,我们用properties文件来代替XML,以简化操作。
2 AOP核心原理V1.0版本
AOP的基本实现原理是利用动态代理机制,创建一个新的代理类完成代码织入,以达到代码功能增强的目的。如果各位小伙伴对动态代理原理不太了解的话,可以回看一下我前段时间更新的“设计模式就该这样学”系列中的动态代理模式专题文章。那么Spring AOP又是如何利用动态代理工作的呢?其实Spring主要功能就是完成解耦,将我们需要增强的代码逻辑单独拆离出来放到专门的类中,然后,通过声明配置文件来关联这些已经被拆离的逻辑,最后合并到一起运行。Spring容器为了保存这种关系,我们可以简单的理解成Spring是用一个Map保存保存这种关联关系的。Map的key就是我们要调用的目标方法,Map的value就是我们要织入的方法。只不过要织入的方法有前后顺序,因此我们需要标记织入方法的位置。在目标方法前面织入的逻辑叫做前置通知,在目标方法后面织入的逻辑叫后置通知,在目标方法出现异常时需要织入的逻辑叫异常通知。Map的具体设计如下:
private Map<Method,Map<String, Method>> methodAdvices = new HashMap<Method, Map<String, Method>>();
下面我完整的写出一个简易的ApplicationContex,小伙伴可以参考 一下:
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import java.util.Properties;import java.util.regex.Matcher;import java.util.regex.Pattern;public class GPApplicationContext {private Properties contextConfig = new Properties();private Map<String,Object> ioc = new HashMap<String,Object>();//用来保存配置文件中对应的Method和Advice的对应关系private Map<Method,Map<String, Method>> methodAdvices = new HashMap<Method, Map<String, Method>>();public GPApplicationContext(){//为了演示,手动初始化一个Beanioc.put("memberService", new MemberService());doLoadConfig("application.properties");doInitAopConfig();}public Object getBean(String name){return createProxy(ioc.get(name));}private Object createProxy(Object instance){return new GPJdkDynamicAopProxy(instance).getProxy();}//加载配置文件private void doLoadConfig(String contextConfigLocation) {//直接从类路径下找到Spring主配置文件所在的路径//并且将其读取出来放到Properties对象中//相对于scanPackage=com.gupaoedu.demo 从文件中保存到了内存中InputStream is = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);try {contextConfig.load(is);} catch (IOException e) {e.printStackTrace();}finally {if(null != is){try {is.close();} catch (IOException e) {e.printStackTrace();}}}}private void doInitAopConfig() {try {Class apectClass = Class.forName(contextConfig.getProperty("aspectClass"));Map<String,Method> aspectMethods = new HashMap<String,Method>();for (Method method : apectClass.getMethods()) {aspectMethods.put(method.getName(),method);}//PonintCut 表达式解析为正则表达式String pointCut = contextConfig.getProperty("pointCut").replaceAll("\\\\.","\\\\\\\\.").replaceAll("\\\\\\\\.\\\\*",".*").replaceAll("\\\\(","\\\\\\\\(").replaceAll("\\\\)","\\\\\\\\)");Pattern pointCutPattern = Pattern.compile(pointCut);for (Map.Entry<String,Object> entry : ioc.entrySet()) {Class<?> clazz = entry.getValue().getClass();//循环找到所有的方法for (Method method : clazz.getMethods()) {//保存方法名String methodString = method.toString();if(methodString.contains("throws")){methodString = methodString.substring(0,methodString.lastIndexOf("throws")).trim();}Matcher matcher = pointCutPattern.matcher(methodString);if(matcher.matches()){Map<String,Method> advices = new HashMap<String,Method>();if(!(null == contextConfig.getProperty("aspectBefore") || "".equals( contextConfig.getProperty("aspectBefore")))){advices.put("before",aspectMethods.get(contextConfig.getProperty("aspectBefore")));}if(!(null == contextConfig.getProperty("aspectAfter") || "".equals( contextConfig.getProperty("aspectAfter")))){advices.put("after",aspectMethods.get(contextConfig.getProperty("aspectAfter")));}if(!(null == contextConfig.getProperty("aspectAfterThrow") || "".equals( contextConfig.getProperty("aspectAfterThrow")))){advices.put("afterThrow",aspectMethods.get(contextConfig.getProperty("aspectAfterThrow")));}methodAdvices.put(method,advices);}}}}catch (Exception e){e.printStackTrace();}}class GPJdkDynamicAopProxy implements GPInvocationHandler {private Object instance;public GPJdkDynamicAopProxy(Object instance) {this.instance = instance;}public Object getProxy() {return Proxy.newProxyInstance(instance.getClass().getClassLoader(),instance.getClass().getInterfaces(),this);}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object aspectObject = Class.forName(contextConfig.getProperty("aspectClass")).newInstance();Map<String,Method> advices = methodAdvices.get(instance.getClass().getMethod(method.getName(),method.getParameterTypes()));Object returnValue = null;advices.get("before").invoke(aspectObject);try {returnValue = method.invoke(instance, args);}catch (Exception e){advices.get("afterThrow").invoke(aspectObject);e.printStackTrace();throw e;}advices.get("after").invoke(aspectObject);return returnValue;}}}
测试代码:
public class MemberServiceTest {public static void main(String[] args) {GPApplicationContext applicationContext = new GPApplicationContext();IMemberService memberService = (IMemberService)applicationContext.getBean("memberService");try {memberService.get("1");memberService.save(new Member());} catch (Exception e) {e.printStackTrace();}}}
我们通过简单几百行代码,就可以完整地演示Spring AOP的核心原理,是不是很简单呢?当然,小伙伴们还是要自己动手哈亲自体验一下,这样才会印象深刻。下面,我们继续完善,将Spring AOP 1.0升级到2.0,那么2.0版本我是完全仿真Spring的原始设计来写的,希望能够给大家带来不一样的手写体验,从而更加深刻地理解Spring AOP的原理。
3 完成AOP顶层设计
3.1 GPJoinPoint
定义一个切点的抽象,这是AOP的基础组成单元。我们可以理解为这是某一个业务方法的附加信息。可想而知,切点应该包含业务方法本身、实参列表和方法所属的实例对象,还可以在GPJoinPoint中添加自定义属性,看下面的代码:
package com.tom.spring.formework.aop.aspect;import java.lang.reflect.Method;/*** 回调连接点,通过它可以获得被代理的业务方法的所有信息*/public interface GPJoinPoint {Method getMethod(); //业务方法本身Object[] getArguments(); //该方法的实参列表Object getThis(); //该方法所属的实例对象//在JoinPoint中添加自定义属性void setUserAttribute(String key, Object value);//从已添加的自定义属性中获取一个属性值Object getUserAttribute(String key);}
3.2 GPMethodInterceptor
方法拦截器是AOP代码增强的基本组成单元,其子类主要有GPMethodBeforeAdvice、GPAfterReturningAdvice和GPAfterThrowingAdvice。
package com.tom.spring.formework.aop.intercept;/*** 方法拦截器顶层接口*/public interface GPMethodInterceptor{Object invoke(GPMethodInvocation mi) throws Throwable;}
3.3 GPAopConfig
定义AOP的配置信息的封装对象,以方便在之后的代码中相互传递。
package com.tom.spring.formework.aop;import lombok.Data;/*** AOP配置封装*/@Datapublic class GPAopConfig {//以下配置与properties文件中的属性一一对应private String pointCut; //切面表达式private String aspectBefore; //前置通知方法名private String aspectAfter; //后置通知方法名private String aspectClass; //要织入的切面类private String aspectAfterThrow; //异常通知方法名private String aspectAfterThrowingName; //需要通知的异常类型}
3.4 GPAdvisedSupport
GPAdvisedSupport主要完成对AOP配置的解析。其中pointCutMatch()方法用来判断目标类是否符合切面规则,从而决定是否需要生成代理类,对目标方法进行增强。而getInterceptorsAndDynamic- InterceptionAdvice()方法主要根据AOP配置,将需要回调的方法封装成一个拦截器链并返回提供给外部获取。
package com.tom.spring.formework.aop.support;import com.tom.spring.formework.aop.GPAopConfig;import com.tom.spring.formework.aop.aspect.GPAfterReturningAdvice;import com.tom.spring.formework.aop.aspect.GPAfterThrowingAdvice;import com.tom.spring.formework.aop.aspect.GPMethodBeforeAdvice;import java.lang.reflect.Method;import java.util.*;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** 主要用来解析和封装AOP配置*/public class GPAdvisedSupport {private Class targetClass;private Object target;private Pattern pointCutClassPattern;private transient Map<Method, List<Object>> methodCache;private GPAopConfig config;public GPAdvisedSupport(GPAopConfig config){this.config = config;}public Class getTargetClass() {return targetClass;}public void setTargetClass(Class targetClass) {this.targetClass = targetClass;parse();}public Object getTarget() {return target;}public void setTarget(Object target) {this.target = target;}public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, Class<?> targetClass) throws Exception {List<Object> cached = methodCache.get(method);//缓存未命中,则进行下一步处理if (cached == null) {Method m = targetClass.getMethod(method.getName(),method.getParameterTypes());cached = methodCache.get(m);//存入缓存this.methodCache.put(m, cached);}return cached;}public boolean pointCutMatch(){return pointCutClassPattern.matcher(this.targetClass.toString()).matches();}private void parse(){//pointCut表达式String pointCut = config.getPointCut().replaceAll("\\\\.","\\\\\\\\.").replaceAll("\\\\\\\\.\\\\*",".*").replaceAll("\\\\(","\\\\\\\\(").replaceAll("\\\\)","\\\\\\\\)");String pointCutForClass = pointCut.substring(0,pointCut.lastIndexOf("\\\\(") - 4);pointCutClassPattern = Pattern.compile("class " + pointCutForClass.substring (pointCutForClass.lastIndexOf(" ")+1));methodCache = new HashMap<Method, List<Object>>();Pattern pattern = Pattern.compile(pointCut);try {Class aspectClass = Class.forName(config.getAspectClass());Map<String,Method> aspectMethods = new HashMap<String,Method>();for (Method m : aspectClass.getMethods()){aspectMethods.put(m.getName(),m);}//在这里得到的方法都是原生方法for (Method m : targetClass.getMethods()){String methodString = m.toString();if(methodString.contains("throws")){methodString = methodString.substring(0,methodString.lastIndexOf("throws")).trim();}Matcher matcher = pattern.matcher(methodString);if(matcher.matches()){//能满足切面规则的类,添加到AOP配置中List<Object> advices = new LinkedList<Object>();//前置通知if(!(null == config.getAspectBefore() || "".equals(config.getAspectBefore().trim()))) {advices.add(new GPMethodBeforeAdvice(aspectMethods.get (config.getAspectBefore()), aspectClass.newInstance()));}//后置通知if(!(null == config.getAspectAfter() || "".equals(config.getAspectAfter(). trim()))) {advices.add(new GPAfterReturningAdvice(aspectMethods.get (config.getAspectAfter()), aspectClass.newInstance()));}//异常通知if(!(null == config.getAspectAfterThrow() || "".equals(config.getAspectAfterThrow().trim()))) {GPAfterThrowingAdvice afterThrowingAdvice = new GPAfterThrowingAdvice (aspectMethods.get(config.getAspectAfterThrow()), aspectClass.newInstance());afterThrowingAdvice.setThrowingName(config.getAspectAfterThrowingName());advices.add(afterThrowingAdvice);}methodCache.put(m,advices);}}} catch (Exception e) {e.printStackTrace();}}}
3.5 GPAopProxy
GPAopProxy是代理工厂的顶层接口,其子类主要有两个:GPCglibAopProxy和GPJdkDynamicAopProxy,分别实现CGlib代理和JDK Proxy代理。
package com.tom.spring.formework.aop;/*** 代理工厂的顶层接口,提供获取代理对象的顶层入口*///默认就用JDK动态代理public interface GPAopProxy {//获得一个代理对象Object getProxy();//通过自定义类加载器获得一个代理对象Object getProxy(ClassLoader classLoader);}
3.6 GPCglibAopProxy
本文未实现CglibAopProxy,感兴趣的“小伙伴”可以自行尝试。
package com.tom.spring.formework.aop;import com.tom.spring.formework.aop.support.GPAdvisedSupport;/*** 使用CGlib API生成代理类,在此不举例* 感兴趣的“小伙伴”可以自行实现*/public class GPCglibAopProxy implements GPAopProxy {private GPAdvisedSupport config;public GPCglibAopProxy(GPAdvisedSupport config){this.config = config;}@Overridepublic Object getProxy() {return null;}@Overridepublic Object getProxy(ClassLoader classLoader) {return null;}}
3.7 GPJdkDynamicAopProxy
下面来看GPJdkDynamicAopProxy的实现,主要功能在invoke()方法中。从代码量来看其实不多,主要是调用了GPAdvisedSupport的getInterceptorsAndDynamicInterceptionAdvice()方法获得拦截器链。在目标类中,每一个被增强的目标方法都对应一个拦截器链。
package com.tom.spring.formework.aop;import com.tom.spring.formework.aop.intercept.GPMethodInvocation;import com.tom.spring.formework.aop.support.GPAdvisedSupport;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.List;/*** 使用JDK Proxy API生成代理类*/public class GPJdkDynamicAopProxy implements GPAopProxy,InvocationHandler {private GPAdvisedSupport config;public GPJdkDynamicAopProxy(GPAdvisedSupport config){this.config = config;}//把原生的对象传进来public Object getProxy(){return getProxy(this.config.getTargetClass().getClassLoader());}@Overridepublic Object getProxy(ClassLoader classLoader) {return Proxy.newProxyInstance(classLoader,this.config.getTargetClass().getInterfaces(),this);}//invoke()方法是执行代理的关键入口@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//将每一个JoinPoint也就是被代理的业务方法(Method)封装成一个拦截器,组合成一个拦截器链List<Object> interceptorsAndDynamicMethodMatchers = config.getInterceptorsAndDynamicInterceptionAdvice(method,this.config.getTargetClass());//交给拦截器链MethodInvocation的proceed()方法执行GPMethodInvocation invocation = new GPMethodInvocation(proxy,this.config.getTarget(), method,args,this.config.getTargetClass(),interceptorsAndDynamicMethodMatchers);return invocation.proceed();}}
从代码中可以看出,从GPAdvisedSupport中获得的拦截器链又被当作参数传入GPMethodInvocation的构造方法中。那么GPMethodInvocation中到底又对方法链做了什么呢?
3.8 GPMethodInvocation
GPMethodInvocation的代码如下:
package com.tom.spring.formework.aop.intercept;import com.tom.spring.formework.aop.aspect.GPJoinPoint;import java.lang.reflect.Method;import java.util.List;/*** 执行拦截器链,相当于Spring中ReflectiveMethodInvocation的功能*/public class GPMethodInvocation implements GPJoinPoint {private Object proxy; //代理对象private Method method; //代理的目标方法private Object target; //代理的目标对象private Class<?> targetClass; //代理的目标类private Object[] arguments; //代理的方法的实参列表private List<Object> interceptorsAndDynamicMethodMatchers; //回调方法链//保存自定义属性private Map<String, Object> userAttributes;private int currentInterceptorIndex = -1;public GPMethodInvocation(Object proxy, Object target, Method method, Object[] arguments,Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {this.proxy = proxy;this.target = target;this.targetClass = targetClass;this.method = method;this.arguments = arguments;this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;}public Object proceed() throws Throwable {//如果Interceptor执行完了,则执行joinPointif (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {return this.method.invoke(this.target,this.arguments);}Object interceptorOrInterceptionAdvice =this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);//如果要动态匹配joinPointif (interceptorOrInterceptionAdvice instanceof GPMethodInterceptor) {GPMethodInterceptor mi = (GPMethodInterceptor) interceptorOrInterceptionAdvice;return mi.invoke(this);} else {//执行当前Intercetporreturn proceed();}}@Overridepublic Method getMethod() {return this.method;}@Overridepublic Object[] getArguments() {return this.arguments;}@Overridepublic Object getThis() {return this.target;}public void setUserAttribute(String key, Object value) {if (value != null) {if (this.userAttributes == null) {this.userAttributes = new HashMap<String,Object>();}this.userAttributes.put(key, value);}else {if (this.userAttributes != null) {this.userAttributes.remove(key);}}}public Object getUserAttribute(String key) {return (this.userAttributes != null ? this.userAttributes.get(key) : null);}}
从代码中可以看出,proceed()方法才是MethodInvocation的关键所在。在proceed()中,先进行判断,如果拦截器链为空,则说明目标方法无须增强,直接调用目标方法并返回。如果拦截器链不为空,则将拦截器链中的方法按顺序执行,直到拦截器链中所有方法全部执行完毕。
4 设计AOP基础实现
4.1 GPAdvice
GPAdvice作为所有回调通知的顶层接口设计,在Mini版本中为了尽量和原生Spring保持一致,只是被设计成了一种规范,并没有实现任何功能。
/*** 回调通知顶层接口*/public interface GPAdvice {}
4.2 GPAbstractAspectJAdvice
使用模板模式设计GPAbstractAspectJAdvice类,封装拦截器回调的通用逻辑,主要封装反射动态调用方法,其子类只需要控制调用顺序即可。
package com.tom.spring.formework.aop.aspect;import java.lang.reflect.Method;/*** 封装拦截器回调的通用逻辑,在Mini版本中主要封装了反射动态调用方法*/public abstract class GPAbstractAspectJAdvice implements GPAdvice {private Method aspectMethod;private Object aspectTarget;public GPAbstractAspectJAdvice(Method aspectMethod, Object aspectTarget) {this.aspectMethod = aspectMethod;this.aspectTarget = aspectTarget;}//反射动态调用方法protected Object invokeAdviceMethod(GPJoinPoint joinPoint,Object returnValue,Throwable ex)throws Throwable {Class<?> [] paramsTypes = this.aspectMethod.getParameterTypes();if(null == paramsTypes || paramsTypes.length == 0) {return this.aspectMethod.invoke(aspectTarget);}else {Object[] args = new Object[paramsTypes.length];for (int i = 0; i < paramsTypes.length; i++) {if(paramsTypes[i] == GPJoinPoint.class){args[i] = joinPoint;}else if(paramsTypes[i] == Throwable.class){args[i] = ex;}else if(paramsTypes[i] == Object.class){args[i] = returnValue;}}return this.aspectMethod.invoke(aspectTarget,args);}}}
4.3 GPMethodBeforeAdvice
GPMethodBeforeAdvice继承GPAbstractAspectJAdvice,实现GPAdvice和GPMethodInterceptor接口,在invoke()中控制前置通知的调用顺序。
package com.tom.spring.formework.aop.aspect;import com.tom.spring.formework.aop.intercept.GPMethodInterceptor;import com.tom.spring.formework.aop.intercept.GPMethodInvocation;import java.lang.reflect.Method;/*** 前置通知具体实现*/public class GPMethodBeforeAdvice extends GPAbstractAspectJAdvice implements GPAdvice, GPMethodInterceptor {private GPJoinPoint joinPoint;public GPMethodBeforeAdvice(Method aspectMethod, Object target) {super(aspectMethod, target);}public void before(Method method, Object[] args, Object target) throws Throwable {invokeAdviceMethod(this.joinPoint,null,null);}public Object invoke(GPMethodInvocation mi) throws Throwable {this.joinPoint = mi;this.before(mi.getMethod(), mi.getArguments(), mi.getThis());return mi.proceed();}}
4.4 GPAfterReturningAdvice
GPAfterReturningAdvice继承GPAbstractAspectJAdvice,实现GPAdvice和GPMethodInterceptor接口,在invoke()中控制后置通知的调用顺序。
package com.tom.spring.formework.aop.aspect;import com.tom.spring.formework.aop.intercept.GPMethodInterceptor;import com.tom.spring.formework.aop.intercept.GPMethodInvocation;import java.lang.reflect.Method;/*** 后置通知具体实现*/public class GPAfterReturningAdvice extends GPAbstractAspectJAdvice implements GPAdvice, GPMethodInterceptor {private GPJoinPoint joinPoint;public GPAfterReturningAdvice(Method aspectMethod, Object target) {super(aspectMethod, target);}@Overridepublic Object invoke(GPMethodInvocation mi) throws Throwable {Object retVal = mi.proceed();this.joinPoint = mi;this.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());return retVal;}public void afterReturning(Object returnValue, Method method, Object[] args,Object target) throws Throwable{invokeAdviceMethod(joinPoint,returnValue,null);}}
4.5 GPAfterThrowingAdvice
GPAfterThrowingAdvice继承GPAbstractAspectJAdvice,实现GPAdvice和GPMethodInterceptor接口,在invoke()中控制异常通知的调用顺序。
package com.tom.spring.formework.aop.aspect;import com.tom.spring.formework.aop.intercept.GPMethodInterceptor;import com.tom.spring.formework.aop.intercept.GPMethodInvocation;import java.lang.reflect.Method;/*** 异常通知具体实现*/public class GPAfterThrowingAdvice extends GPAbstractAspectJAdvice implements GPAdvice, GPMethodInterceptor {private String throwingName;private GPMethodInvocation mi;public GPAfterThrowingAdvice(Method aspectMethod, Object target) {super(aspectMethod, target);}public void setThrowingName(String name) {this.throwingName = name;}@Overridepublic Object invoke(GPMethodInvocation mi) throws Throwable {try {return mi.proceed();}catch (Throwable ex) {invokeAdviceMethod(mi,null,ex.getCause());throw ex;}}}
感兴趣的“小伙伴”可以参看Spring源码,自行实现环绕通知的调用逻辑。
4.6 接入getBean()方法
在上面的代码中,我们已经完成了Spring AOP模块的核心功能,那么接下如何集成到IoC容器中去呢?找到GPApplicationContext的getBean()方法,我们知道getBean()中负责Bean初始化的方法其实就是instantiateBean(),在初始化时就可以确定是否返回原生Bean或Proxy Bean。代码实现如下:
//传一个BeanDefinition,返回一个实例Beanprivate Object instantiateBean(GPBeanDefinition beanDefinition){Object instance = null;String className = beanDefinition.getBeanClassName();try{//因为根据Class才能确定一个类是否有实例if(this.singletonBeanCacheMap.containsKey(className)){instance = this.singletonBeanCacheMap.get(className);}else{Class<?> clazz = Class.forName(className);instance = clazz.newInstance();GPAdvisedSupport config = instantionAopConfig(beanDefinition);config.setTargetClass(clazz);config.setTarget(instance);if(config.pointCutMatch()) {instance = createProxy(config).getProxy();}this.factoryBeanObjectCache.put(className,instance);this.singletonBeanCacheMap.put(beanDefinition.getFactoryBeanName(),instance);}return instance;}catch (Exception e){e.printStackTrace();}return null;}private GPAdvisedSupport instantionAopConfig(GPBeanDefinition beanDefinition) throws Exception{GPAopConfig config = new GPAopConfig();config.setPointCut(reader.getConfig().getProperty("pointCut"));config.setAspectClass(reader.getConfig().getProperty("aspectClass"));config.setAspectBefore(reader.getConfig().getProperty("aspectBefore"));config.setAspectAfter(reader.getConfig().getProperty("aspectAfter"));config.setAspectAfterThrow(reader.getConfig().getProperty("aspectAfterThrow"));config.setAspectAfterThrowingName(reader.getConfig().getProperty("aspectAfterThrowingName"));return new GPAdvisedSupport(config);}private GPAopProxy createProxy(GPAdvisedSupport config) {Class targetClass = config.getTargetClass();if (targetClass.getInterfaces().length > 0) {return new GPJdkDynamicAopProxy(config);}return new GPCglibAopProxy(config);}
从上面的代码中可以看出,在instantiateBean()方法中调用createProxy()决定代理工厂的调用策略,然后调用代理工厂的proxy()方法创建代理对象。最终代理对象将被封装到BeanWrapper中并保存到IoC容器。
5 织入业务代码
通过前面的代码编写,所有的核心模块和底层逻辑都已经实现,“万事俱备,只欠东风。”接下来,该是“见证奇迹的时刻了”。我们来织入业务代码,做一个测试。创建LogAspect类,实现对业务方法的监控。主要记录目标方法的调用日志,获取目标方法名、实参列表、每次调用所消耗的时间。
5.1 LogAspect
LogAspect的代码如下:
package com.tom.spring.demo.aspect;import com.tom.spring.formework.aop.aspect.GPJoinPoint;import lombok.extern.slf4j.Slf4j;import java.util.Arrays;/*** 定义一个织入的切面逻辑,也就是要针对目标代理对象增强的逻辑* 本类主要完成对方法调用的监控,监听目标方法每次执行所消耗的时间*/@Slf4jpublic class LogAspect {//在调用一个方法之前,执行before()方法public void before(GPJoinPoint joinPoint){joinPoint.setUserAttribute("startTime_" + joinPoint.getMethod().getName(),System.currentTimeMillis());//这个方法中的逻辑是由我们自己写的log.info("Invoker Before Method!!!" +"\\nTargetObject:" + joinPoint.getThis() +"\\nArgs:" + Arrays.toString(joinPoint.getArguments()));}//在调用一个方法之后,执行after()方法public void after(GPJoinPoint joinPoint){log.info("Invoker After Method!!!" +"\\nTargetObject:" + joinPoint.getThis() +"\\nArgs:" + Arrays.toString(joinPoint.getArguments()));long startTime = (Long) joinPoint.getUserAttribute("startTime_" + joinPoint.getMethod().getName());long endTime = System.currentTimeMillis();System.out.println("use time :" + (endTime - startTime));}public void afterThrowing(GPJoinPoint joinPoint, Throwable ex){log.info("出现异常" +"\\nTargetObject:" + joinPoint.getThis() +"\\nArgs:" + Arrays.toString(joinPoint.getArguments()) +"\\nThrows:" + ex.getMessage());}}
通过上面的代码可以发现,每一个回调方法都加了一个参数GPJoinPoint,还记得GPJoinPoint为何物吗?事实上,GPMethodInvocation就是GPJoinPoint的实现类。而GPMethodInvocation又是在GPJdkDynamicAopPorxy的invoke()方法中实例化的,即每个被代理对象的业务方法会对应一个GPMethodInvocation实例。也就是说,MethodInvocation的生命周期是被代理对象中业务方法的生命周期的对应。前面我们已经了解,调用GPJoinPoint的setUserAttribute()方法可以在GPJoinPoint中自定义属性,调用getUserAttribute()方法可以获取自定义属性的值。在LogAspect的before()方法中,在GPJoinPoint中设置了startTime并赋值为系统时间,即记录方法开始调用时间到MethodInvocation的上下文。在LogAspect的after()方法中获取startTime,再次获取的系统时间保存到endTime。在AOP拦截器链回调中,before()方法肯定在after()方法之前调用,因此两次获取的系统时间会形成一个时间差,这个时间差就是业务方法执行所消耗的时间。通过这个时间差,就可以判断业务方法在单位时间内的性能消耗,是不是设计得非常巧妙?事实上,市面上几乎所有的系统监控框架都是基于这样一种思想来实现的,可以高度解耦并减少代码侵入。
5.2 IModifyService
为了演示异常回调通知,我们给之前定义的IModifyService接口的add()方法添加了抛出异常的功能,看下面的代码实现:
package com.tom.spring.demo.service;/*** 增、删、改业务*/public interface IModifyService {/*** 增加*/String add(String name, String addr) throws Exception;/*** 修改*/String edit(Integer id, String name);/*** 删除*/String remove(Integer id);}
5.3 ModifyService
ModifyService的代码如下:
package com.tom.spring.demo.service.impl;import com.tom.spring.demo.service.IModifyService;import com.tom.spring.formework.annotation.GPService;/*** 增、删、改业务*/@GPServicepublic class ModifyService implements IModifyService {/*** 增加*/public String add(String name,String addr) throws Exception {throw new Exception("故意抛出异常,测试切面通知是否生效");// return "modifyService add,name=" + name + ",addr=" + addr;}/*** 修改*/public String edit(Integer id,String name) {return "modifyService edit,id=" + id + ",name=" + name;}/*** 删除*/public String remove(Integer id) {return "modifyService id=" + id;}}
6 运行效果演示
在浏览器中输入 http://localhost/web/add.json?name=Tom&addr=HunanChangsha ,就可以直观明了地看到Service层抛出的异常信息,如下图所示。
控制台输出如下图所示。
通过控制台输出,可以看到异常通知成功捕获异常信息,触发了GPMethodBeforeAdvice 和GPAfterThrowingAdvice,而并未触发GPAfterReturningAdvice,符合我们的预期。下面再做一个测试,输入 http://localhost/web/query.json?name=Tom ,结果如下图所示:
控制台输出如下图所示:
通过控制台输出可以看到,分别捕获了前置通知、后置通知,并打印了相关信息,符合我们的预期。
至此AOP模块大功告成,是不是有一种小小的成就感,跃跃欲试?在整个Mini版本实现中有些细节没有过多考虑,更多的是希望给“小伙伴们”提供一种学习源码的思路。手写源码不是为了重复造轮子,也不是为了装“高大上”,其实只是我们推荐给大家的一种学习方式。
本文为“Tom弹架构”原创,转载请注明出处。技术在于分享,我分享我快乐!
如果本文对您有帮助,欢迎关注和点赞;如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。
原创不易,坚持很酷,都看到这里了,小伙伴记得点赞、收藏、在看,一键三连加关注!如果你觉得内容太干,可以分享转发给朋友滋润滋润!