老生谈spring(十五):invokeBeanFactoryPostProcessors
1、上一节给大家展示了BeanFactoryPostProcessor的作用,现在给大家讲解它是如何生效的。前面几节已经带大家清楚地看到,开启了注解模式以后就会扫描指定包的所有含Component注解的class。所以只要加了Component的class都会被扫描器扫描到,扫描完了以后就会进入refresh的invokeBeanFactoryPostProcessors方法:
2、invokeBeanFactoryPostProcessors方法调用的是PostProcessorRegistrationDelegate的invokeBeanFactoryPostProcessors,这个方法比较长我们只看精华部分。beanFactory的getBeanNamesForType方法获取到所有的BeanFactoryPostProcessor的beanName。
//从beanFactory中获取所有实现了BeanFactoryPostProcessor的class的beanName
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
}
else if (b