Spring注解--AOP原理(二):AnnotationAwareAspectJAutoProxyCreator

1. AnnotationAwareAspectJAutoProxyCreator结构分析

  • 继承关系: AnnotationAwareAspectJAutoProxyCreator->AspectJAwareAdvisorAutoProxyCreator->AbstractAdvisorAutoProxyCreator->
    AbstractAutoProxyCreator->SmartInstantiationAwareBeanPostProcessorBeanFactoryAware
    可以发现AnnotationAwareAspectJAutoProxyCreator其实是一个后置处理器,后置处理器的初始化与注册可以参考: Spring注解--AOP原理(三):BeanPostProcessor创建与注册

2. AnnotationAwareAspectJAutoProxyCreator执行时机

AnnotationAwareAspectJAutoProxyCreator 同时继承了InstantiationAwareBeanPostProcessor,BeanPostProcessor接口。那么AnnotationAwareAspectJAutoProxyCreator既可以在bean创建前后,也可以在初始化前后进行拦截操作。

  • 在创建bean前后的拦截
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
    ...
     try {
    // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
        Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
        if (bean != null) {
            return bean;
        }
      }
    ...
    //如果不是InstantiationPostBean,就会创建bean,然后返回
    Object beanInstance = doCreateBean(beanName, mbdToUse, args);
}
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
        Object bean = null;
        if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
            // Make sure bean class is actually resolved at this point.
            if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
                Class targetType = determineTargetType(beanName, mbd);
                if (targetType != null) {
                    bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                    if (bean != null) {
                        bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                    }
                }
            }
            mbd.beforeInstantiationResolved = (bean != null);
        }
        return bean;
    }
  • 在bean的初始化前后进行拦截
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
    ...
       if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
       }
        try {
            invokeInitMethods(beanName, wrappedBean, mbd);
        }
        catch (Throwable ex) {
            throw new BeanCreationException(
                    (mbd != null ? mbd.getResourceDescription() : null),
                    beanName, "Invocation of init method failed", ex);
        }
        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }
}

3. AnnotationAwareAspectJAutoProxyCreator的作用

在AnnotationAwareAspectJAutoProxyCreator的继承关系中,有一个AbstractAutoProxyCreator类。该类定义了下面两个方法,它们分别拦截目标bean创建,和初始化流程。

@Override
    public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
        Object cacheKey = getCacheKey(beanClass, beanName);
        if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
            if (this.advisedBeans.containsKey(cacheKey)) {
                return null;
            }
            if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
                this.advisedBeans.put(cacheKey, Boolean.FALSE);
                return null;
            }
        }
        // Create proxy here if we have a custom TargetSource.
        // Suppresses unnecessary default instantiation of the target bean:
        // The TargetSource will handle target instances in a custom fashion.
        if (beanName != null) {
            TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
            if (targetSource != null) {
                this.targetSourcedBeans.add(beanName);
                Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
                Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
                this.proxyTypes.put(cacheKey, proxy.getClass());
                return proxy;
            }
        }

        return null;
    }

//在创建其他bean时,调用该方法,创建代理的bean,从而将AOP方式注入的方法,注入到目标bean中
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean != null) {
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        if (!this.earlyProxyReferences.contains(cacheKey)) {
                return wrapIfNecessary(bean, beanName, cacheKey);
        }
    }
    return bean;
}

postProcessAfterInitialization方法中的wrapIfNecessary方法,会为目标对象动态地创建JdkDynamicAopProxy或者ObjenesisCglibAopProxy的代理对象

你可能感兴趣的:(Spring注解--AOP原理(二):AnnotationAwareAspectJAutoProxyCreator)