registerBeanPostProcessors(beanFactory)
/**
* 实例化并注册bean中所有BeanPostProcessor
* Instantiate and register all BeanPostProcessor beans,
* respecting explicit order if given.
* <p>Must be called before any instantiation of application beans.
*/
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
// 实例化所有的BeanPostProcessor并将其注册到BeanFactory中
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
// 获取beanFactory中所有的BeanPostProcessor类型的bean的名字,此处为空
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// 当BeanPostProcessor实例化过程中有bean被创建,则注册一个BeanPostProcessorChecker记录信息
// 也就是说一个bean没有资格被所有的BeanPostProcessor处理
// 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;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
// 对所有的BeanPostProcessor根据实现不同的接口(PriorityOrdered,Ordered等)进行分离
// 用于存放实现了PriorityOrdered.class的后处理器集合
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
// 用于存放实现了PriorityOrdered.class并且是MergedBeanDefinitionPostProcessor类型的后处理器集合
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
// 用于存放实现了Ordered.class的后处理器集合
List<String> orderedPostProcessorNames = new ArrayList<>();
// 用于存放其它情况的后处理器集合
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, register the BeanPostProcessors that implement PriorityOrdered.
// 首先,注册实现了ProorityOrdered.class接口的BeanPostProcessor
// 排序
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 注册(将BeanPostProcessor加入到BeanFactory中beanFactory.addBeanPostProcessor(postProcessor))
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered.
// 其次注册实现了Ordered.class接口的BeanPostProcessor
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors.
// 然后,注册剩余的所有普通的BeanPostProcessor
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors.
// 最后,重新注册所有内部的BeanPostProcessor
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
// 将检查内部bean的后置处理器重新注册为应用监听器
// 将其移动到processor处理链的末尾(用来应对代理等)
// 此处创建了一个应用监听器并放到了处理链的最后
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
找出所有bean的后置处理器BeanPostProcessor(注意是bean的后置处理器,不是beanFactory的后置处理器,bean的后置处理器处理的是bean的实例,beanFactory处理的是bean的定义)然后将这些bean的后置处理器分为三类注册到BeanFactory中
- 实现了顺序接口Ordered.class的,先放入orderedPostProcessor集合,排序后顺序加入beanFactory的后处理集合中
- 实现了优先级接口PriorityOrdered.class的,顺序排序后加入beanFactory的后处理集合中
- 既没实现Ordered.class也没实现PriorityIrdered.class的后置处理器,也加入到beanFactory后处理集合中