registerBeanPostProcessors注册后置处理器
对于方法执行的先后顺序可看之前文章
一、在调用注册后置处理器方法之前我们先看看截至当前,服务内总共有哪些后置处理器,都是在什么时机下被放到容器的什么地方
001 、加载系统默认beanDefinition 这个文章里面我们可以看到,首先加入的beanDefinition中有些是属于BeanPostProcessor类型的,当前beanFactory中信息如下,系统默认beanDefinition 存储在beanDefinitionMap中和beanDefinitonNames中,数据结构分别为Map和List
002、在prepareBeanFactory(beanFactory)方法中,此时放入的是ApplicationContextAwareProcessor对象,进入代码
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
this.beanPostProcessors.remove(beanPostProcessor);
this.beanPostProcessors.add(beanPostProcessor);
if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
this.hasInstantiationAwareBeanPostProcessors = true;
}
if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
this.hasDestructionAwareBeanPostProcessors = true;
}
}
由上代码可以看到,放入了beanPostProcessors属性中,数据结构List
003、在postProcessBeanFactory(beanFactory)方法中,加入了ServletContextAwareProcessor对象
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig);
}
至此我们能看到当前beanFacory中信息如下
备注:对于方法执行先后可查看之前的文章
二、进入registerBeanPostProcessors方法内部
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
又用到了委托类PostProcessorRegistrationDelegate
beanFactory.getBeanPostProcessorCount()方法获取的是beanPostProcessors属性的长度(List),代码中之所以+1是因为后面它又加入了一个BeanPostProcessorChecker
@Override
public int getBeanPostProcessorCount() {
return this.beanPostProcessors.size();
}
然后创建了四个List,根据有无顺序进行放入
List priorityOrderedPostProcessors = new ArrayList();
List internalPostProcessors = new ArrayList();
List orderedPostProcessorNames = new ArrayList();
List nonOrderedPostProcessorNames = new ArrayList();
进入 registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
方法
private static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {
for (BeanPostProcessor postProcessor : postProcessors) {
beanFactory.addBeanPostProcessor(postProcessor);
}
}
又进行了一遍后置处理器的放入工作,其实主要就是把之前beanDefinitonNames这个list中的后置处理器复制到beanPostProcessors集合中而已
最后加入了ApplicationListenerDetector后置处理器
最终,容器中的后置处理器如下