乐趣区

Spring5源码分析5ConfigurationClassPostProcessor-上

接上回,我们讲到了 refresh() 方法中的 invokeBeanFactoryPostProcessors(beanFactory) 方法主要在执行 BeanFactoryPostProcessor 和其子接口 BeanDefinitionRegistryPostProcessor 的方法。

在创建 AnnotationConfigApplicationContext 对象时 Spring 就添加了一个非常重要的 BeanFactoryPostProcessor 接口实现类:ConfigurationClassPostProcessor。注意,这里说的添加只是添加到容器的 beanDefinitionMap 中,还没有创建真正的实例 Bean。

简单回顾一下 ConfigurationClassPostProcessor 是在什么时候被添加到容器中的:在 AnnotationConfigApplicationContext 的无参构造器中创建 AnnotatedBeanDefinitionReader 对象时会向传入的 BeanDefinitionRegistry 中注册解析注解配置类相关的 processors 的 BeanDefinitionConfigurationClassPostProcessor 就是在此处被添加到容器中的。


ConfigurationClassPostProcessor

先看一些 ConfigurationClassPostProcessor 的继承体系:

ConfigurationClassPostProcessor实现了 BeanDefinitionRegistryPostProcessor 接口,也就拥有了在 Spring 容器启动时,往容器中注册 BeanDefinition 的能力。

我们知道,ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry方法是在 refresh(); 方法中的 invokeBeanFactoryPostProcessors(beanFactory); 中被执行的,下面我们就一起来看一下该方法。

ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {int registryId = System.identityHashCode(registry);
    if (this.registriesPostProcessed.contains(registryId)) {
        throw new IllegalStateException("postProcessBeanDefinitionRegistry already called on this post-processor against" + registry);
    }
    if (this.factoriesPostProcessed.contains(registryId)) {
        throw new IllegalStateException("postProcessBeanFactory already called on this post-processor against" + registry);
    }
    this.registriesPostProcessed.add(registryId);

    processConfigBeanDefinitions(registry);
}

主要的逻辑在 processConfigBeanDefinitions(registry); 中,点开源码:

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
    // 获取所有的 BeanDefinitionName
    String[] candidateNames = registry.getBeanDefinitionNames();

    for (String beanName : candidateNames) {BeanDefinition beanDef = registry.getBeanDefinition(beanName);

        // https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/core.html#beans-java-basic-concepts
        // Full @Configuration vs“lite”@Bean mode
        if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||
                ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) {if (logger.isDebugEnabled()) {logger.debug("Bean definition has already been processed as a configuration class:" + beanDef);
            }
        }

        // 校验是否为配置类
        // 配置类分为两种 Full @Configuration vs“lite”@Bean mode
        // 校验之后在 BeanDefinition 中添加标志属性
        // 如果满足条件则加入到 configCandidates
        else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
            // 如果是配置类, 就放到 configCandidates 变量中
            configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
        }
    }

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

    // Sort by previously determined @Order value, if applicable
    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;
    // 传入的 registry 是 DefaultListableBeanFactory
    if (registry instanceof SingletonBeanRegistry) {sbr = (SingletonBeanRegistry) registry;
        if (!this.localBeanNameGeneratorSet) {
            // 获取自定义 BeanNameGenerator, 一般情况下为空
            BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
            if (generator != null) {
                this.componentScanBeanNameGenerator = generator;
                this.importBeanNameGenerator = generator;
            }
        }
    }

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

    // Parse each @Configuration class
    // new ConfigurationClassParser, 用来解析 @Configuration 类
    ConfigurationClassParser parser = new ConfigurationClassParser(
            this.metadataReaderFactory, this.problemReporter, this.environment,
            this.resourceLoader, this.componentScanBeanNameGenerator, registry);

    // 将 configCandidates 转成 set  candidates , 去重
    Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
    Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
    do {
        // 解析配置类
        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());
        }
        // Import 类,@Bean,@ImportResource 转化为 BeanDefinition
        this.reader.loadBeanDefinitions(configClasses);
        alreadyParsed.addAll(configClasses);

        candidates.clear();
        // 再获取一下容器中 BeanDefinition 的数据, 如果发现数量增加了, 说明有新的 BeanDefinition 被注册了
        if (registry.getBeanDefinitionCount() > candidateNames.length) {String[] newCandidateNames = registry.getBeanDefinitionNames();
            Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(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());
    }

    if (this.metadataReaderFactory instanceof 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) this.metadataReaderFactory).clearCache();}
}

获取所有的 BeanDefinitionNames,然后循环这个数组,判断其是否为配置类。

前 5 个是 Spring 注册的内置 processor,最后一个是传入给 AnnotationConfigApplicationContext 的配置类AppConfig.class

在 Spring 中存在两种 ConfigurationClass,一种是 FullConfigurationClass 另一种是LiteConfigurationClass。关于这两者的区别,可以参看笔者文章之前关于 Full @Configuration 和 lite @Bean mode 的文章。

ConfigurationClassUtils#checkConfigurationClassCandidate方法内部就是在判断属于哪种配置类,并在 BeanDefinition 中标记判断结果。其具体的判断逻辑如下:

public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) {return metadata.isAnnotated(Configuration.class.getName());
}
private static final Set<String> candidateIndicators = new HashSet<>(8);

static {candidateIndicators.add(Component.class.getName());
    candidateIndicators.add(ComponentScan.class.getName());
    candidateIndicators.add(Import.class.getName());
    candidateIndicators.add(ImportResource.class.getName());
}

public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
    // Do not consider an interface or an annotation...
    if (metadata.isInterface()) {return false;}

    // Any of the typical annotations found?
    for (String indicator : candidateIndicators) {if (metadata.isAnnotated(indicator)) {return true;}
    }

    // Finally, let's look for @Bean methods...
    try {return metadata.hasAnnotatedMethods(Bean.class.getName());
    } catch (Throwable ex) {if (logger.isDebugEnabled()) {logger.debug("Failed to introspect @Bean methods on class [" + metadata.getClassName() + "]:" + ex);
        }
        return false;
    }
}

判断 configCandidates 变量中存放的

配置类是否为空,如果不为空,则对其进行排序。

创建 ConfigurationClassParser 对象,用于解析 @Configuration 类,完成包的扫描、BeanDefinition的注册。主要通过执行 parser.parse(candidates); 方法来完成。

执行 parser.parse(candidates) 方法前:

执行 parser.parse(candidates) 方法后:

解析完配置类之后,紧接着又执行了 this.reader.loadBeanDefinitions(configClasses); 方法。这个方法主要是用来处理 Import 类@Bean@ImportResource注解。关于这两个方法的具体细节,我们下次再讲。

最后又加了入了对 ImportAware 接口支持所需要的 Bean。

// 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());
}

关于对 ImportAware 接口的使用,我们也下次再讲。


未完待续 ……

源码学习笔记:https://github.com/shenjianen…

个人公众号:

退出移动版