关于spring:spring-AOP过程

7次阅读

共计 14673 个字符,预计需要花费 37 分钟才能阅读完成。

spring AOP

spring 通过 @EnableAspectJAutoProxy 引入开启 AOP,理论心愿引入一个 bean,交给 context 治理。bean 继承了 BeanPostProcess,通过后置处理器,对所有的 bean 能匹配到增强器的进行解决。
这个类就是 AnnotationAwareAspectJAutoProxyCreator。

一个 bean 是怎么被 AnnotationAwareAspectJAutoProxyCreator 加强的。

步骤 1. 在以后容器中找个合乎该 bean 的拦截器
步骤 2. 依据这 bean 和拦截器生成代理。

AbstractAutoProxyCreator#postProcessAfterInitialization 后置处理器。

    public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {if (bean != null) {Object cacheKey = getCacheKey(bean.getClass(), beanName);
            if (this.earlyProxyReferences.remove(cacheKey) != bean) {return wrapIfNecessary(bean, beanName, cacheKey); // 外围解决
            }
        }
        return bean;
    }

AbstractAutoProxyCreator#wrapIfNecessary

protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {return bean;}
        if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {return bean;}
        if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {this.advisedBeans.put(cacheKey, Boolean.FALSE);
            return bean;
        }

        // Create proxy if we have advice.
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);  // 获取以后类的拦截器
        if (specificInterceptors != DO_NOT_PROXY) {this.advisedBeans.put(cacheKey, Boolean.TRUE);
            Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
            this.proxyTypes.put(cacheKey, proxy.getClass());
            return proxy;
        }

        this.advisedBeans.put(cacheKey, Boolean.FALSE);
        return bean;
    }

总结:在以后容器中找到这个 bean 匹配的拦截器,如果有,则进行解决创立 proxyBean,否则返回原始的 bean。

怎么在以后容器中找个合乎该 bean 的拦截器

  1. 在容器容器中找到所有的 advisor.class 类的 bean
    间接通过 BeanFactoryUtils#beanNamesForTypeIncludingAncestors, 获取想要 advisor 类型的所有 bean 的名称。
  2. 找到所有的 AspectJ-annotated aspect bean,构建成 advisor。
    遍历所有的获取所有的 Bean 的,判断 bean 是否被 Aspect.class 注解。如果是,则构建一个 advisor 对象,不便当前间接应用。
    BeanFactoryAspectJAdvisorsBuilder#buildAspectJAdvisors
    获取两类 advisor,一类是 before,around 的 PointcutAdvisor 等办法,一类是 introduction 的 DeclareParentsAdvisor(用来给类增加办法的)。

    if (this.advisorFactory.isAspect(beanType)) {aspectNames.add(beanName);
                             AspectMetadata amd = new AspectMetadata(beanType, beanName);
                             if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
                                 MetadataAwareAspectInstanceFactory factory =
                                         new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
                                 List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
                                 if (this.beanFactory.isSingleton(beanName)) {this.advisorsCache.put(beanName, classAdvisors);
                                 }
                                 else {this.aspectFactoryCache.put(beanName, factory);
                                 }
                                 advisors.addAll(classAdvisors);
                             }

    ReflectiveAspectJAdvisorFactory,BeanFactoryAspectInstanceFactory。看看怎么结构 advisor 的。

ReflectiveAspectJAdvisorFactory#getAdvisor

    public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory,
            int declarationOrderInAspect, String aspectName) {validate(aspectInstanceFactory.getAspectMetadata().getAspectClass());

        AspectJExpressionPointcut expressionPointcut = getPointcut(candidateAdviceMethod, aspectInstanceFactory.getAspectMetadata().getAspectClass());
        if (expressionPointcut == null) {return null;}

        return new InstantiationModelAwarePointcutAdvisorImpl(expressionPointcut, candidateAdviceMethod,
                this, aspectInstanceFactory, declarationOrderInAspect, aspectName);
    }

以后容器的内的所有的 advisor 应该是初始化,并存在在对应的缓存中。当前每次都重缓存中获取。

  1. 以后类去匹配所有的 advisor 中的
    将指标类的办法和 advisor 的 PointcutExpression 进行匹配,返回所有匹配的 advisor、
    将放回的 advisor 进行排序,根据类的 order。

通过 ProxyFactory 创立代理对象。

  1. 拦截器曾经找到。指标对象 bean 也有了。
    protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
            @Nullable Object[] specificInterceptors, TargetSource targetSource) {if (this.beanFactory instanceof ConfigurableListableBeanFactory) {AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
        }

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.copyFrom(this);

        if (!proxyFactory.isProxyTargetClass()) {if (shouldProxyTargetClass(beanClass, beanName)) {  // 判断是否代理类,而不是接口
                proxyFactory.setProxyTargetClass(true);
            }
            else {evaluateProxyInterfaces(beanClass, proxyFactory); 
            }
        }

        Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
        proxyFactory.addAdvisors(advisors);
        proxyFactory.setTargetSource(targetSource);
        customizeProxyFactory(proxyFactory);

        proxyFactory.setFrozen(this.freezeProxy);
        if (advisorsPreFiltered()) {proxyFactory.setPreFiltered(true);
        }

        return proxyFactory.getProxy(getProxyClassLoader());
    }

proxyFactory 中蕴含代理类,委托外部的 DefaultAopProxyFactory()执行创立代理对象。

    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {Class<?> targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class:" +
                        "Either an interface or a target is required for proxy creation.");
            }
            if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {return new JdkDynamicAopProxy(config);
            }
            return new ObjenesisCglibAopProxy(config);
        }
        else {return new JdkDynamicAopProxy(config);
        }
    }

如果代理的接口,应用 JdkDynamicAopProxy,如果是类,应用 ObjenesisCglibAopProxy

JdkDynamicAopProxy 生成代理的过程

JdkDynamicAopProxy 实现了 jdk 的 InvocationHandler。所以代理的解决逻辑在外部的 invoke 办法外部

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);
        findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
        return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); // 传入了本人做拦截器。}

关键点:创立代理的拦截器执行什么代码
JdkDynamicAopProxy#

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 {if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {// The target does not implement the equals(Object) method itself.
                return equals(args[0]);
            }
            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);
            }
            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;

            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 = targetSource.getTarget();
            Class<?> targetClass = (target != null ? target.getClass() : null);

            // Get the interception chain for this method.
            List<Object> 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...
                MethodInvocation invocation =
                        new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); // 通过反射代用代理办法。// Proceed to the joinpoint through the interceptor chain.
                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.releaseTarget(target);
            }
            if (setProxyContext) {
                // Restore old proxy.
                AopContext.setCurrentProxy(oldProxy);
            }
        }
    }

真正执行代理办法的中央。
ReflectiveMethodInvocation # 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); // 解决 before,after 执行,看 MethodBeforeAdviceInterceptor。}
    }

执行剖析: 怎么解决 before,after,afterReturn 等办法呢?

MethodBeforeAdviceInterceptor#invoke

public Object invoke(MethodInvocation mi) throws Throwable {this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
        return mi.proceed();}

AfterReturningAdviceInterceptor# invoke

public Object invoke(MethodInvocation mi) throws Throwable {Object retVal = mi.proceed();
        this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
        return retVal;
    }

ThrowsAdviceInterceptor#invoke

    public Object invoke(MethodInvocation mi) throws Throwable {
        try {return mi.proceed();
        }
        catch (Throwable ex) {Method handlerMethod = getExceptionHandler(ex);
            if (handlerMethod != null) {invokeHandlerMethod(mi, ex, handlerMethod);
            }
            throw ex;
        }
    }

AspectJAfterAdvice#invoke

public Object invoke(MethodInvocation mi) throws Throwable {
        try {return mi.proceed();
        }
        finally {invokeAdviceMethod(getJoinPointMatch(), null, null);
        }
    }

AspectJAroundAdvice#invoke

    public Object invoke(MethodInvocation mi) throws Throwable {if (!(mi instanceof ProxyMethodInvocation)) {throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation:" + mi);
        }
        ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi;
        ProceedingJoinPoint pjp = lazyGetProceedingJoinPoint(pmi);
        JoinPointMatch jpm = getJoinPointMatch(pmi);
        return invokeAdviceMethod(pjp, jpm, null, null);
    }

能够显著看出,应用的递归的调用。执行后果和每一个类型的 interceptor 无关。
对于 before 的办法是进入递归栈的办法先执行,对于 after 的办法是递归返回时的执行程序。
before -> after – > before1 -> after2.
办法执行程序为,before -> before1 -> after2 -> after.

CglibAopProxy 生成代理过程

CglibAopProxy # getProxy

public Object getProxy(@Nullable ClassLoader classLoader) {if (logger.isTraceEnabled()) {logger.trace("Creating CGLIB proxy:" + this.advised.getTargetSource());
        }

        try {Class<?> rootClass = this.advised.getTargetClass();
            Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");

            Class<?> proxySuperClass = rootClass;
            if (rootClass.getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)) {proxySuperClass = rootClass.getSuperclass();
                Class<?>[] additionalInterfaces = rootClass.getInterfaces();
                for (Class<?> additionalInterface : additionalInterfaces) {this.advised.addInterface(additionalInterface);
                }
            }

            // Validate the class, writing log messages as necessary.
            validateClassIfNecessary(proxySuperClass, classLoader);

            // Configure CGLIB Enhancer...
            Enhancer enhancer = createEnhancer();
            if (classLoader != null) {enhancer.setClassLoader(classLoader);
                if (classLoader instanceof SmartClassLoader &&
                        ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {enhancer.setUseCache(false);
                }
            }
            enhancer.setSuperclass(proxySuperClass);
            enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
            enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
            enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader));

            Callback[] callbacks = getCallbacks(rootClass); // 外围,获取了哪些回调办法。外围获取了 DynamicAdvisedInterceptor,加强的代码执行解决。Class<?>[] types = new Class<?>[callbacks.length];
            for (int x = 0; x < types.length; x++) {types[x] = callbacks[x].getClass();}
            // fixedInterceptorMap only populated at this point, after getCallbacks call above
            enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset)); // 过滤器,决定办法被哪一个 callback 执行。enhancer.setCallbackTypes(types);

            // Generate the proxy class and create a proxy instance.
            return createProxyClassAndInstance(enhancer, callbacks);
        }
        catch (CodeGenerationException | IllegalArgumentException ex) {throw new AopConfigException("Could not generate CGLIB subclass of" + this.advised.getTargetClass() +
                    ": Common causes of this problem include using a final class or a non-visible class",
                    ex);
        }
        catch (Throwable ex) {// TargetSource.getTarget() failed
            throw new AopConfigException("Unexpected AOP exception", ex);
        }
    }

Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised); // 获取了外围拦截器

DynamicAdvisedInterceptor # intercept

    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
            Object oldProxy = null;
            boolean setProxyContext = false;
            Object target = null;
            TargetSource targetSource = this.advised.getTargetSource();
            try {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 = targetSource.getTarget();
                Class<?> targetClass = (target != null ? target.getClass() : null);
                List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
                Object retVal;
                // Check whether we only have one InvokerInterceptor: that is,
                // no real advice, but just reflective invocation of the target.
                if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
                    // 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 = methodProxy.invoke(target, argsToUse);
                }
                else {
                    // We need to create a method invocation...
                    retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed(); // 逻辑和 jdk 代理雷同,调用 chain 中的办法。}
                retVal = processReturnType(proxy, target, method, retVal);
                return retVal;
            }
            finally {if (target != null && !targetSource.isStatic()) {targetSource.releaseTarget(target);
                }
                if (setProxyContext) {
                    // Restore old proxy.
                    AopContext.setCurrentProxy(oldProxy);
                }
            }
        }

CglibMethodInvocation 继承 ReflectiveMethodInvocation,所以执行的过程和 jdk 代理是一样的。

正文完
 0