6. Spring启动流程-invokeBeanFactoryPostProcessors

6. Spring启动流程-invokeBeanFactoryPostProcessors

invokeBeanFactoryPostProcessors

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, this.getBeanFactoryPostProcessors());
        if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean("loadTimeWeaver")) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }

    }

PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors

执行post方法的类

此时有三个明显的BeanFactoryPostProcessor
Springboot准备过程已经添加好了这三个

0 = {SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor@4085} 
1 = {ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor@4086} 
2 = {ConfigFileApplicationListener$PropertySourceOrderingPostProcessor@4087} 

这三个看着没啥用,不分析了
然而下边还有其他的更重要的BeanFactoryPostProcesser

invokeBeanDefinitionRegistryPostProcessors

关键代码就这两句

.....
//获取到了一个叫org.springframework.context.annotation.internalConfigurationAnnotationProcessor的bean
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
....
//实例化一下
 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
 .....
 //执行一下注册方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);

由于逻辑比较复杂,具体逻辑请转入《6.1 Spring启动流程-internalConfigurationAnnotationProcessor》

CachingMetadataReaderFactoryPostProcessor

简单看一下CachingMetadataReaderFactoryPostProcessor 干了啥吧

 public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
            this.register(registry);
            this.configureConfigurationClassPostProcessor(registry);
        }

        private void register(BeanDefinitionRegistry registry) {
            BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(SharedMetadataReaderFactoryContextInitializer.SharedMetadataReaderFactoryBean.class, SharedMetadataReaderFactoryContextInitializer.SharedMetadataReaderFactoryBean::new).getBeanDefinition();
            registry.registerBeanDefinition("org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory", definition);
        }
         private void configureConfigurationClassPostProcessor(BeanDefinitionRegistry registry) {
            try {
                //在这里给internalConfigurationAnnotationProcessor添加了一个metadataReaderFactory属性
                BeanDefinition definition = registry.getBeanDefinition("org.springframework.context.annotation.internalConfigurationAnnotationProcessor");
                definition.getPropertyValues().add("metadataReaderFactory", new RuntimeBeanReference("org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory"));
            } catch (NoSuchBeanDefinitionException var3) {
                ;
            }
        }

经过他之后加了一个

6 = "org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory"
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 6. Spring启动流程-invokeBeanFactoryPostProcessors