Spring容器(四): BeanFactoryPostProcessor方法调用

在创建、准备BeanFactory环境的流程结束后,在invokeBeanFactoryPostProcessors方法中,spring开始查找项目中定义的BeanFactoryPostProcessor,按照PriorityOrdered->Ordered->normal 顺序调用BeanFactoryPostProcessor的方法,为容器注入新的类:比如BeanPostProcessor(并没有初始化,只是保存了类名)。

1. invokeBeanFactoryPostProcessors 方法

invokeBeanFactoryPostProcessors(
            ConfigurableListableBeanFactory beanFactory, List beanFactoryPostProcessors){

    if (beanFactory instanceof BeanDefinitionRegistry) {
        ...
        //找到BeanFactory创建之初,spring默认添加的ConfigurationClassPostProcessor
       String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);

        //优先执行implement了PriorityOrdered 的 BeanDefinitionRegistryPostProcessor
        List priorityOrderedPostProcessors = new ArrayList();
            for (String ppName : postProcessorNames) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
        sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
        registryPostProcessors.addAll(priorityOrderedPostProcessors);
        invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);

        //执行implement了Ordered 的 BeanDefinitionRegistryPostProcessor
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        List orderedPostProcessors = new ArrayList();
        for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        sortPostProcessors(beanFactory, orderedPostProcessors);
            registryPostProcessors.addAll(orderedPostProcessors);
        invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
            ... 
            //执行常规的BeanDefinitionRegistryPostProcessor 和BeanPostProcessor
         invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
         invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    }

    //同样按照PriorityOrdered->Ordered->normal   的顺序 执行非BeanDefinitionRegistryPostProcessor 的BeanFactoryPostProcessor 的方法。
    .... 省略...
}

2. ConfigurationClassPostProcessor

在创建BeanFactory时,spring默认添加了ConfigurationClassPostProcessor。


ConfigurationClassPostProcessor

ConfigurationClassPostProcessor implement 了BeanDefinitionRegistryPostProcessor 和 PriorityOrdered 接口,于是在 invokeBeanFactoryPostProcessors方法调用的过程中,会调用ConfigurationClassPostProcessor 的 postProcessBeanDefinitionRegistry, 进而在容器中注入新的类:ImportAwareBeanPostProcessor,EnhancedConfigurationBeanPostProcessor:

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
      ...
      RootBeanDefinition iabpp = new RootBeanDefinition(ImportAwareBeanPostProcessor.class);
      iabpp.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
      registry.registerBeanDefinition(IMPORT_AWARE_PROCESSOR_BEAN_NAME, iabpp);
      RootBeanDefinition ecbpp = new RootBeanDefinition(EnhancedConfigurationBeanPostProcessor.class);
      ecbpp.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
      registry.registerBeanDefinition(ENHANCED_CONFIGURATION_PROCESSOR_BEAN_NAME, ecbpp);
      ...
      // 注入MainConfig类中用@Bean注解的类到容器中
      processConfigBeanDefinitions(registry);
}
  • processConfigBeanDefinitions
public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
        ...
        // Read the model and create bean definitions based on its content
    if (this.reader == null) {
        this.reader = new ConfigurationClassBeanDefinitionReader(
        registry, this.sourceExtractor, this.resourceLoader, this.environment,
        this.importBeanNameGenerator, parser.getImportRegistry());
    }
    this.reader.loadBeanDefinitions(configClasses);
}
  • loadBeanDefinitionsForConfigurationClass: 在容器中注入MainConfig中通过@Bean,@Import,以及ImportBeanDefinitionRegistrars 方式添加的类。
private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configClass,
            TrackedConditionEvaluator trackedConditionEvaluator) {

        if (trackedConditionEvaluator.shouldSkip(configClass)) {
            String beanName = configClass.getBeanName();
            if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
                this.registry.removeBeanDefinition(beanName);
            }
            this.importRegistry.removeImportingClassFor(configClass.getMetadata().getClassName());
            return;
        }
           
        if (configClass.isImported()) {
            registerBeanDefinitionForImportedConfigurationClass(configClass);
        }
        for (BeanMethod beanMethod : configClass.getBeanMethods()) {
            loadBeanDefinitionsForBeanMethod(beanMethod);
        }
        loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
        loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
    }

你可能感兴趣的:(Spring容器(四): BeanFactoryPostProcessor方法调用)