文章目录
- 一、回顾@EnableAspectJAutoProxy
- 二、AbstractAutoProxyCreator类
- 三、AbstractAdvisorAutoProxyCreator类
- 四、AspectJAwareAdvisorAutoProxyCreator类
- 五、AnnotationAwareAspectJAutoProxyCreator类
一、回顾@EnableAspectJAutoProxy
在上一章中,通过查看@EnableAspectJAutoProxy 注解的源码,如下所示:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
得知,@EnableAspectJAutoProxy注解是通过使用@Import(AspectJAutoProxyRegistrar.class) 给容器中注册一个名字叫做internalAutoProxyCreator = AnnotationAwareAspectJAutoProxyCreator的组件。
并且也分析了AnnotationAwareAspectJAutoProxyCreato类的核心继承关系,如下所示:
AnnotationAwareAspectJAutoProxyCreator
->AspectJAwareAdvisorAutoProxyCreator(父类)
->AbstractAdvisorAutoProxyCreator(父类)
->AbstractAutoProxyCreator(父类)
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware(两个接口)
查看继承关系可以发现,此类实现了Aware与BeanPostProcessor接口,这两个接口都和Spring bean的初始化有关,由此推测此类主要处理方法都来自这两个接口的实现方法。同时该类也实现了order方法。
- BeanPostProcessor:后置处理器,即在bean初始化完成前后做些事情
- BeanFactoryAware:自动注入BeanFactory
接下来就看看AnnotationAwareAspectJAutoProxyCreator 类的调用流程,具体来说,就是看看 AnnotationAwareAspectJAutoProxyCreator 作为BeanPostProcessor做了哪些工作,作为BeanFactoryAware做了哪些工作。
二、AbstractAutoProxyCreator类
在 AnnotationAwareAspectJAutoProxyCreator类的继承关系上可以看出, 是在AbstractAutoProxyCreator类开始实现 SmartInstantiationAwareBeanPostProcessor接口和 BeanFactoryAware 接口的。
所以,先从 AbstractAutoProxyCreator 类进行分析。
由 AbstractAutoProxyCreator 类的定义可以看出,AbstractAutoProxyCreator类直接实现了SmartInstantiationAwareBeanPostProcessor 接口和 BeanFactoryAware 接口。
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
...
}
既然 AbstractAutoProxyCreator 实现了 BeanFactoryAware 接口, 那么 AbstractAutoProxyCreator 类中就一定存在setBeanFactory()方法,如下所示:
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Nullable
protected BeanFactory getBeanFactory() {
return this.beanFactory;
}
另外,在 AbstractAutoProxyCreator 类中还存在与BeanPostProcessor后置处理器有关的方法,分别为:postProcessBeforeInstantiation()、postProcessAfterInstantiation()、postProcessProperties()、postProcessBeforeInitialization()、postProcessAfterInitialization()。整体源代码如下所示:
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
Object cacheKey = getCacheKey(beanClass, beanName);
if (!StringUtils.hasLength(beanName) || !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;
}
}
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
if (StringUtils.hasLength(beanName)) {
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;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) {
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
return pvs;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
@Override
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 类中看到了 BeanFactoryAware 的实现和 BeanPostProcessor后置处理器的实现。
接下来,我们再来看看AbstractAutoProxyCreator 的子类 AbstractAdvisorAutoProxyCreator类。
三、AbstractAdvisorAutoProxyCreator类
在 AbstractAdvisorAutoProxyCreator类中,我们会看到如下代码:
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException(
"AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: " + beanFactory);
}
initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
}
说明在AbstractAdvisorAutoProxyCreator类中重写了setBeanFactory()方法。并且在AbstractAdvisorAutoProxyCreator类的setBeanFactory()方法中,首先会调用AbstractAutoProxyCreator 类中的setBeanFactory()方法。
在setBeanFactory()方法中还会调用initBeanFactory()方法,initBeanFactory()方法的实现如下所示:
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
this.advisorRetrievalHelper = new BeanFactoryAdvisorRetrievalHelperAdapter(beanFactory);
}
另外,我们并没有在AbstractAdvisorAutoProxyCreator类中找到与后置处理器相关的方法。
接下来,我们继续分析AbstractAdvisorAutoProxyCreator类的子类AspectJAwareAdvisorAutoProxyCreator类。
四、AspectJAwareAdvisorAutoProxyCreator类
通过查看AspectJAwareAdvisorAutoProxyCreator类的源码,我们得知,在 AspectJAwareAdvisorAutoProxyCreator类中没有与后置处理器相关的代码。所以,我们继续向上分析 AspectJAwareAdvisorAutoProxyCreator类的子类 AnnotationAwareAspectJAutoProxyCreator。
五、AnnotationAwareAspectJAutoProxyCreator类
在 AnnotationAwareAspectJAutoProxyCreator类中,我们可以找到一个initBeanFactory()方法,如下所示:
@Override
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.initBeanFactory(beanFactory);
if (this.aspectJAdvisorFactory == null) {
this.aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(beanFactory);
}
this.aspectJAdvisorsBuilder =
new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);
}
看到这里,小伙伴们对于setBeanFactory的调用流程有点清晰了吧?其实setBeanFactory()的调用流程为:首先会执行 AbstractAdvisorAutoProxyCreator类中的setBeanFactory()方法,在AbstractAdvisorAutoProxyCreator类中的setBeanFactory()方法中会调用其父类AbstractAutoProxyCreator 中的setBeanFactory()方法,然后在AbstractAdvisorAutoProxyCreator类中的setBeanFactory()方法中调用initBeanFactory()方法。由于在子类AnnotationAwareAspectJAutoProxyCreator中重写了initBeanFactory()方法,最终调用的就是AnnotationAwareAspectJAutoProxyCreator类中的initBeanFactory()方法。在AnnotationAwareAspectJAutoProxyCreator中首先会执行super.initBeanFactory(beanFactory);调用父类AbstractAdvisorAutoProxyCreator的initBeanFactory()方法。
这么说有点绕,我们来看一张图吧。
注意,上图中的AbstractAdvisorAutoProxyCreator类中的setBeanFactory()方法作为程序调用的入口,它会依次调用AbstractAutoProxyCreator#setBeanFactory() 和AnnotationAwareAspectJAutoProxyCreator#initBeanFactory() ,然后,再由AnnotationAwareAspectJAutoProxyCreator#initBeanFactory() 调用 AbstractAdvisorAutoProxyCreator#initBeanFactory()。
除此之外,我们在AnnotationAwareAspectJAutoProxyCreator类中,并没有发现与后置处理器相关的代码了。
好了,以上就是我们分析的有关AnnotationAwareAspectJAutoProxyCreator类的源码。在下一篇文章中,我们开始debug调试这些源代码的具体执行流程。