Spring AOP JdkDynamicAopProxy

所有文章已迁移至csdn,csdn个人主页bugpool.blog.csdn.net

Spring AOP源码目录

Spring AOP源码01:Jdk动态代理底层源码
Spring AOP源码02:ProxyFactory
Spring AOP源码03:JdkDynamicAopProxy
Spring AOP源码04:MethodInvocation 拦截器调用
Spring AOP源码05:DefaultAdvisorAutoProxyCreator
Spring期末考压轴题:当Spring AOP遇上循环依赖
git注释源码地址:https://github.com/chaitou/spring-framework-master.git

前言

如下图,结合第二篇Spring AOP核心源码 ProxyFactory,Spring AOP动态代理有2中生成方式,当代理对象实现了接口且没有配置强制使用cglib代理时,将使用JdkDynamicAopProxy生成代理。反之使用CglibAopProxy生成代理。同时我们在第一篇Jdk动态代理底层源码中已知,Jdk动态代理通过getProxy生成代理,同时$proxy代理对象在调用方法时,将会回调invoke方法进行。因此对于JdkDynamicAopProxy来说,最重要的代码就是分析getProxyinvoke方法。至于生成代理的底层源码,第一篇已经介绍过,这里不再赘述

AopProxy

源码分析

getProxy

下面代码在第一篇Jdk动态代理底层源码中我们手动写过一遍,这里再复习一下

    @Override
    public Object getProxy(@Nullable ClassLoader classLoader) {
        if (logger.isTraceEnabled()) {
            logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
        }
        // 获取代理接口
        Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
        // 判断接口是否又hashCode和equals方法
        findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
        // 使用JDK代理(classLoader, 接口, 当前JdkDynamicAopProxy对象:用于回调invoke和target对象方法)
        return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
    }
  1. classLoader:当前类加载器
  2. proxiedInterfaces:当前代理类所实现的接口数组
  3. this:将自身类对象传给生成的代理,作为代理的属性h,属性h将被用于方法回调触发h.invoke方法,实现增强

invoke

在开始invoke之前,先定一下目标,为了抓住主干,本专题只关心普通增强引介增强等将暂时跳过,另外独立出章节详解,因此遇到引介增强等可以暂时先不纠结

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object oldProxy = null;
        boolean setProxyContext = false;
        // 获取代理目标对象
        TargetSource targetSource = this.advised.targetSource;
        Object target = null;

        try {
            // equals方法处理
            if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
                // The target does not implement the equals(Object) method itself.
                return equals(args[0]);
            }
            // hashCode处理
            else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
                // The target does not implement the hashCode() method itself.
                return hashCode();
            }
            else if (method.getDeclaringClass() == DecoratingProxy.class) {
                // There is only getDecoratedClass() declared -> dispatch to proxy config.
                return AopProxyUtils.ultimateTargetClass(this.advised);
            }
            // Advised接口或者其父接口中定义的方法,直接反射调用
            else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
                    method.getDeclaringClass().isAssignableFrom(Advised.class)) {
                // Service invocations on ProxyConfig with the proxy config...
                return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
            }

            Object retVal;
            // 目标对象内部调用是无法实现增强的,如果exposeProxy设置为true,需要暴露代理
            // ThreadLocal currentProxy
            // 对象是ThreadLocal,在finally后会清除currentProxy
            if (this.advised.exposeProxy) {
                // Make invocation available if necessary.
                oldProxy = AopContext.setCurrentProxy(proxy);
                setProxyContext = true;
            }

            // Get as late as possible to minimize the time we "own" the target,
            // in case it comes from a pool.
            // 以防target来自对象池,所以在创建代理前调用get获取target
            target = targetSource.getTarget();
            Class targetClass = (target != null ? target.getClass() : null);

            // Get the interception chain for this method.
            // 1. 获取拦截器链
            List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

            // Check whether we have any advice. If we don't, we can fallback on direct
            // reflective invocation of the target, and avoid creating a MethodInvocation.
            // 拦截器为空,直接调用切点方法
            if (chain.isEmpty()) {
                // We can skip creating a MethodInvocation: just invoke the target directly
                // Note that the final invoker must be an InvokerInterceptor so we know it does
                // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
                Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
                retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
            }
            else {
                // We need to create a method invocation...
                // 2. 将拦截器统一封装成ReflectiveMethodInvocation
                MethodInvocation invocation =
                        new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
                // Proceed to the joinpoint through the interceptor chain.
                // 3. 执行拦截器链
                retVal = invocation.proceed();
            }

            // Massage return value if necessary.
            Class returnType = method.getReturnType();
            if (retVal != null && retVal == target &&
                    returnType != Object.class && returnType.isInstance(proxy) &&
                    !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
                // Special case: it returned "this" and the return type of the method
                // is type-compatible. Note that we can't help if the target sets
                // a reference to itself in another returned object.
                retVal = proxy;
            }
            else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
                throw new AopInvocationException(
                        "Null return value from advice does not match primitive return type for: " + method);
            }
            return retVal;
        }
        finally {
            if (target != null && !targetSource.isStatic()) {
                // Must have come from TargetSource.
                // 如果自定义了TargetSource,释放target资源池由子类实现
                targetSource.releaseTarget(target);
            }
            if (setProxyContext) {
                // Restore old proxy.
                AopContext.setCurrentProxy(oldProxy);
            }
        }
    }

1. 获取拦截器链

该方法将获取到所有与当前method匹配的advice(增强),跟踪getInterceptorsAndDynamicInterceptionAdvice代码,发现Spring AOP也使用缓存进行提高性能,如果该方法已经获取过拦截器,则直接取缓存,否则通过advisorChainFactory获取拦截器链

    public List getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class targetClass) {
        MethodCacheKey cacheKey = new MethodCacheKey(method);
        // 从缓存中获取
        List cached = this.methodCache.get(cacheKey);
        if (cached == null) {
            // 获取拦截器链
            cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
                    this, method, targetClass);
            // 设置缓存
            this.methodCache.put(cacheKey, cached);
        }
        return cached;
    }

继续跟中getInterceptorsAndDynamicInterceptionAdvice方法

// DefaultAdvisorChainFactory.java
public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {

    @Override
    public List getInterceptorsAndDynamicInterceptionAdvice(
            Advised config, Method method, @Nullable Class targetClass) {

        // This is somewhat tricky... We have to process introductions first,
        // but we need to preserve order in the ultimate list.
        AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
        Advisor[] advisors = config.getAdvisors();
        List interceptorList = new ArrayList<>(advisors.length);
        Class actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
        Boolean hasIntroductions = null;

        // 循环所有切面
        for (Advisor advisor : advisors) {
            if (advisor instanceof PointcutAdvisor) {
                // Add it conditionally.
                PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
                // 校验当前Advisor是否适用于当前对象
                if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
                    MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
                    boolean match;
                    // 校验Advisor是否应用到当前方法上
                    if (mm instanceof IntroductionAwareMethodMatcher) {
                        if (hasIntroductions == null) {
                            hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
                        }
                        match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
                    }
                    else {
                        match = mm.matches(method, actualClass);
                    }
                    // 匹配成功
                    if (match) {
                        // 从advisor中获取拦截器数组
                        MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
                        if (mm.isRuntime()) {
                            // Creating a new object instance in the getInterceptors() method
                            // isn't a problem as we normally cache created chains.
                            for (MethodInterceptor interceptor : interceptors) {
                                // 添加动态拦截器
                                interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
                            }
                        }
                        else {
                            // 添加普通拦截器
                            interceptorList.addAll(Arrays.asList(interceptors));
                        }
                    }
                }
            }
            // 引介增强
            else if (advisor instanceof IntroductionAdvisor) {
                IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
                if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
                    Interceptor[] interceptors = registry.getInterceptors(advisor);
                    interceptorList.addAll(Arrays.asList(interceptors));
                }
            }
            // 其他类型的advisor
            else {
                Interceptor[] interceptors = registry.getInterceptors(advisor);
                interceptorList.addAll(Arrays.asList(interceptors));
            }
        }

        return interceptorList;
    }
    ...
}

剖去引介增强动态增强,我们只关心普通拦截器。整个代码思路还是比较清晰的,获取所有Advisor(切面),通过pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)校验当前代理对象是否匹配该Advisor,再通过pointcutAdvisor.getPointcut().getMethodMatcher()校验是否匹配当前调用method。如果通过校验,则提取advisor中的interceptors拦截器,添加到interceptorList

2. 将拦截器封装成ReflectiveMethodInvocation

一个构造器的调用,重点在下一步调用

    protected ReflectiveMethodInvocation(
            Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments,
            @Nullable Class targetClass, List interceptorsAndDynamicMethodMatchers) {

        this.proxy = proxy;
        this.target = target;
        this.targetClass = targetClass;
        this.method = BridgeMethodResolver.findBridgedMethod(method);
        this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments);
        this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
    }

3. 执行拦截器链

// 执行拦截器链
retVal = invocation.proceed();
    public Object proceed() throws Throwable {
        // We start with an index of -1 and increment early.
        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
            return invokeJoinpoint();
        }

        Object interceptorOrInterceptionAdvice =
                this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
        if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
            // Evaluate dynamic method matcher here: static part will already have
            // been evaluated and found to match.
            // 动态匹配
            InterceptorAndDynamicMethodMatcher dm =
                    (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
            Class targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
            if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
                return dm.interceptor.invoke(this);
            }
            else {
                // Dynamic matching failed.
                // Skip this interceptor and invoke the next in the chain.
                // 不匹配,则跳过
                return proceed();
            }
        }
        else {
            // It's an interceptor, so we just invoke it: The pointcut will have
            // been evaluated statically before this object was constructed.
            // 普通拦截器,直接触发
            return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
        }
    }

同样我们只关心普通拦截器,但是看到普通拦截器仅仅只有一行代码

return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);

这一行代码就可以让拦截器按照Before、After甚至是用户自定义的Order顺序进行链式调用,这也太神奇了吧?下一节我们将挑出AspectJAfterAdviceMethodBeforeAdviceInterceptor探究拦截器是如何实现增强与method之前的有序调用

代理方法调用流程图:


在这里插入图片描述

你可能感兴趣的:(Spring AOP JdkDynamicAopProxy)