承接前文Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors
瞧瞧官方注释
/**
* Instantiate and invoke all registered BeanPostProcessor beans,
* respecting explicit order if given.
* <p>Must be called before any instantiation of application beans.
*/
实例化并且调用所有的已注册的BeanPostProcessor beans,其实也就是简单的把DefaultListableFactory中的所有为BeanPostProcessor的实现bean类放置于其内部属性beanPostProcessors List集合中。
直接源码
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//调用的是委托类的registerBeanPostProcessors方法
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
间接源码-PostProcessorRegistrationDelegate.registerBeanPostProcessors
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//获取beanFactory中的所有实现BeanPostProcessor接口的bean
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
//BeanPostProcessorChecker检查类
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
//此处的实现与上节的invokeBeanFactoryPostProcessors方法一致,这里就省略不赘述了
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
/**省略*/
// Finally, re-register all internal BeanPostProcessors.
OrderComparator.sort(internalPostProcessors);
//主要是调用此方法将所有的postProcessors按照优先级有序的放置在beanFactory的内部属性beanPostProcessors集合中
registerBeanPostProcessors(beanFactory, internalPostProcessors);
//增加ApplicationListenerDetector类
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
小结
- 获取beanFactory中的所有实现postProcessor接口的bean对象按照优先级有序的放置在beanFactory的内部属性beanPostProcessors集合中,但是并没有执行其中的相应公共方法
- 新增两个BeanPostProcessor实现类:BeanPostProcessorChecker为首,ApplicationListenerDetector为尾
下节预告
Spring源码情操陶冶-AbstractApplicationContext#initMessageSource