Spring创建Bean源码 - 加载配置类

《Spring创建Bean源码 - 解析配置类》中已经介绍了Spring是怎么解析配置类的,这个章节主要介绍Spring是怎么将解析后的配置类加载并注册到Spring中的。

以下是包含了解析配置类和加载配置类的代码片段。**this.reader.loadBeanDefinitions(configClasses)**就是将解析后的配置类加载并注册到Spring的代码片段。

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
    List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
    //获取已经注册进来的Bean名称
    String[] candidateNames = registry.getBeanDefinitionNames();

    for (String beanName : candidateNames) {
        BeanDefinition beanDef = registry.getBeanDefinition(beanName);
        //判断某个类中是否存在 configurationClass属性
        if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
            }
        }
        //判断BeanDefinition是否是一个配置类
        else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
            //配置类加入配置集合
            configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
        }
    }

    // Return immediately if no @Configuration classes were found
    if (configCandidates.isEmpty()) {
        return;
    }

    //按照@Order指定的顺序排序BeanDefinition
    configCandidates.sort((bd1, bd2) -> {
        int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());
        int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());
        return Integer.compare(i1, i2);
    });

    // Detect any custom bean name generation strategy supplied through the enclosing application context
    SingletonBeanRegistry sbr = null;
    if (registry instanceof SingletonBeanRegistry) {
        sbr = (SingletonBeanRegistry) registry;
        if (!this.localBeanNameGeneratorSet) {
            BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(
                    AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);
            if (generator != null) {
                this.componentScanBeanNameGenerator = generator;
                this.importBeanNameGenerator = generator;
            }
        }
    }

    if (this.environment == null) {
        this.environment = new StandardEnvironment();
    }

    //创建解析配置类解析对象
    ConfigurationClassParser parser = new ConfigurationClassParser(
            this.metadataReaderFactory, this.problemReporter, this.environment,
            this.resourceLoader, this.componentScanBeanNameGenerator, registry);

    Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
    Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
    do {
        StartupStep processConfig = this.applicationStartup.start("spring.context.config-classes.parse");
        //开始解析配置类
        //该API包含向Spring中注册springboot的AutoConfiguration自动装配类,并将自动装配类放入org.springframework.context.annotation.ConfigurationClassParser.importStack数据结构中
        parser.parse(candidates);
        parser.validate();
        //去重
        Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
        configClasses.removeAll(alreadyParsed);

        // 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());
        }
        //加载所有的配置类,并注册到注册器中
        //读取的配置类ConfigurationClass,将该配置类以及该配置类中@Bean方法中创建的对象封装成BeanDefinition,然后注册到BeanDefinitionRegister中
        //加载带有@ConfigurationProperties注解的类,并封装成BeanDefinition注册到BeanDefinitionRegister中
        this.reader.loadBeanDefinitions(configClasses);

        alreadyParsed.addAll(configClasses);
        processConfig.tag("classCount", () -> String.valueOf(configClasses.size())).end();

        candidates.clear();
        if (registry.getBeanDefinitionCount() > candidateNames.length) {
            String[] newCandidateNames = registry.getBeanDefinitionNames();
            Set<String> oldCandidateNames = Set.of(candidateNames);
            Set<String> alreadyParsedClasses = new HashSet<>();
            for (ConfigurationClass configurationClass : alreadyParsed) {
                alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
            }
            for (String candidateName : newCandidateNames) {
                if (!oldCandidateNames.contains(candidateName)) {
                    BeanDefinition bd = registry.getBeanDefinition(candidateName);
                    if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
                            !alreadyParsedClasses.contains(bd.getBeanClassName())) {
                        candidates.add(new BeanDefinitionHolder(bd, candidateName));
                    }
                }
            }
            candidateNames = newCandidateNames;
        }
    }
    while (!candidates.isEmpty());

    // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
    if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
        sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
    }

    // Store the PropertySourceDescriptors to contribute them Ahead-of-time if necessary
    this.propertySourceDescriptors = parser.getPropertySourceDescriptors();

    if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory cachingMetadataReaderFactory) {
        // Clear cache in externally provided MetadataReaderFactory; this is a no-op
        // for a shared cache since it'll be cleared by the ApplicationContext.
        cachingMetadataReaderFactory.clearCache();
    }
}

  1. BeanDefinitionRegistry中获取所有的BeanDefinition
  2. 挑选出所有的配置类
  3. 按照配置类的Order配置的顺序,对所有的配置类进行排序
  4. 创建配置类解析器ConfigurationClassParser解析每一个配置类并根据配置类中的指定注解派生出更多的配置类,然后为每个配置类生成**ConfigurationClass**对象,将每个配置类解析到的数据封装到对应的ConfigurationClass对象中。
  5. 使用ConfigurationClassBeanDefinitionReader对象,将每一个ConfigurationClass类加载进来,封装成BeanDefinition,注册到BeanDefinitionRegistry中。

加载配置类

/**
 * 读取配置类,并封装成BeanDefinition注册到BeanDefinitionRegister中
 */
public void loadBeanDefinitions(Set<ConfigurationClass> configurationModel) {
    TrackedConditionEvaluator trackedConditionEvaluator = new TrackedConditionEvaluator();
    for (ConfigurationClass configClass : configurationModel) {
        loadBeanDefinitionsForConfigurationClass(configClass, trackedConditionEvaluator);
    }
}

加载配置类的BeanDefinition

/**
 * 读取一个特定的配置类ConfigurationClass,将该配置类以及该配置类中@Bean方法中创建的对象封装成BeanDefinition,然后注册到BeanDefinitionRegister中
 */
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.removeImportingClass(configClass.getMetadata().getClassName());
        return;
    }

    //判断该配置类是否通过@Import注解被注册
    if (configClass.isImported()) {
        registerBeanDefinitionForImportedConfigurationClass(configClass);
    }
    //获取该配置类中带有@Bean注解的方法,然后解析方法,将方法中创建的Bean注册到BeanDefinitionRegister中
    for (BeanMethod beanMethod : configClass.getBeanMethods()) {
        //将@Bean方法所创建的对象的Bean信息加载进来并注册到BeanDefinitionRegister中
        loadBeanDefinitionsForBeanMethod(beanMethod);
    }
    //解析类上@ImportResource注解中配置的配置文件,解析配置文件中的Bean信息,并将Bean信息加载并注册到BeanDefinitionRegister中
    loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
    loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
}

读取配置类ConfigurationClass,将配置类派生出来的配置类注册到注册器中。
详细代码逻辑如下:

  1. @Import注解导入的ConfigurationClass注册到注册器中。
  2. @Bean注解对应的类注册到注册器中。
  3. @ImportedResources注册导入的类注册到注册器中。
  4. 委托**ImportBeanDefinitionRegistrar**注册器进行类注册到。这是一个扩展点。SpringBoot中集成的大多数第三方框架的类都是基于该机制进行注册。

@Import注解导入的ConfigurationClass注册到注册器中

@Bean注解对应的类注册到注册器中

@ImportedResources注册导入的类注册到注册器中

委托ImportBeanDefinitionRegistrar接口进行类注册