主要用于调用BeanPostProcessors的实现,区别于上一个章节,本章节处理的是实例化过后的Bean。
// AbstractApplicationContext.java protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { // 调用BeanPostProcessor PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this); } // PostProcessorRegistrationDelegate.java public static void registerBeanPostProcessors( ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) { // 获取所有的BeanPostProcessor名称 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. // 注册一个BeanPostProcessorChecker,当一个BeanPostProcessor在实例化期间创建一个Bean的时候,打印日志 int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); // 按照优先级整理 // 实现了PriorityOrdered接口的BeanPostProcessor集合 List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); // 内部定义的BeanPostProcessor集合 List<BeanPostProcessor> internalPostProcessors = new ArrayList<>(); // 实现了Ordered接口的BeanPostProcessor名称集合 List<String> orderedPostProcessorNames = new ArrayList<>(); // 未实现排序优先级接口的BeanPostProcessor名称集合 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); } } // 注册实现了PriorityOrdered接口的BeanPostProcessor // First, register the BeanPostProcessors that implement PriorityOrdered. sortPostProcessors(priorityOrderedPostProcessors, beanFactory); registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors); // 注册实现了PriorityOrdered接口的BeanPostProcessor // Next, register the BeanPostProcessors that implement Ordered. List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(); 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); // 注册常规的BeanPostProcessor // Now, register all regular BeanPostProcessors. List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String ppName : nonOrderedPostProcessorNames) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); nonOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); // 重新注册所有的内部BeanPostProcessor // Finally, re-register all internal BeanPostProcessors. sortPostProcessors(internalPostProcessors, beanFactory); registerBeanPostProcessors(beanFactory, internalPostProcessors); // 注册ApplicationListenerDetector,它将在bean初始化完成之后检测是否为ApplicationListener,如果是则加入applicationListeners中 // 在Bean销毁之前,提前从ApplicationEventMulticaster中删除 // Re-register post-processor for detecting inner beans as ApplicationListeners, // moving it to the end of the processor chain (for picking up proxies etc). beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext)); } initMessageSource()(五)SpringBoot启动过程的分析-刷新ApplicationContext (5)
内容版权声明:除非注明,否则皆为本站原创文章。