本文节选自《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(){                   //为了演示,手动初始化一个Bean                     ioc.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;    }    @Override    public Object getProxy() {        return null;    }    @Override    public 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());    }    @Override    public Object getProxy(ClassLoader classLoader) {        return Proxy.newProxyInstance(classLoader,this.config.getTargetClass().getInterfaces(),this);    }    //invoke()办法是执行代理的要害入口    @Override    public 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执行完了,则执行joinPoint        if (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 {//执行以后Intercetpor            return proceed();        }    }    @Override    public Method getMethod() {        return this.method;    }    @Override    public Object[] getArguments() {        return this.arguments;    }    @Override    public 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);    }    @Override    public 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;    }    @Override    public 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... ,就能够直观明了地看到Service层抛出的异样信息,如下图所示。

控制台输入如下图所示。

通过控制台输入,能够看到异样告诉胜利捕捉异样信息,触发了GPMethodBeforeAdvice 和GPAfterThrowingAdvice,而并未触发GPAfterReturningAdvice,合乎咱们的预期。
上面再做一个测试,输出 http://localhost/web/query.js... ,后果如下图所示:

控制台输入如下图所示:

通过控制台输入能够看到,别离捕捉了前置告诉、后置告诉,并打印了相干信息,合乎咱们的预期。

至此AOP模块功败垂成,是不是有一种小小的成就感,蠢蠢欲动?在整个Mini版本实现中有些细节没有过多思考,更多的是心愿给“小伙伴们”提供一种学习源码的思路。手写源码不是为了反复造轮子,也不是为了装“高大上”,其实只是咱们举荐给大家的一种学习形式。

关注微信公众号『 Tom弹架构 』回复“Spring”可获取残缺源码。

本文为“Tom弹架构”原创,转载请注明出处。技术在于分享,我分享我高兴!如果您有任何倡议也可留言评论或私信,您的反对是我保持创作的能源。关注微信公众号『 Tom弹架构 』可获取更多技术干货!

原创不易,保持很酷,都看到这里了,小伙伴记得点赞、珍藏、在看,一键三连加关注!如果你感觉内容太干,能够分享转发给敌人滋润滋润!