Spring5 源码学习 ConfigurationClassPostProcessor (上)
Posted Coder小黑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring5 源码学习 ConfigurationClassPostProcessor (上)相关的知识,希望对你有一定的参考价值。
中讲到了refresh()
方法中的invokeBeanFactoryPostProcessors(beanFactory)
方法主要在执行BeanFactoryPostProcessor
和其子接口BeanDefinitionRegistryPostProcessor
的方法。
在创建AnnotationConfigApplicationContext
对象时Spring就添加了一个非常重要的BeanFactoryPostProcessor
接口实现类:ConfigurationClassPostProcessor
。注意,这里说的添加只是添加到容器的beanDefinitionMap
中,还没有创建真正的实例Bean。
简单回顾一下ConfigurationClassPostProcessor
是在什么时候被添加到容器中的:在AnnotationConfigApplicationContext
的无参构造器中创建AnnotatedBeanDefinitionReader
对象时会向传入的BeanDefinitionRegistry
中注册解析注解配置类相关的processors的BeanDefinition
,ConfigurationClassPostProcessor
就是在此处被添加到容器中的。
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
。关于这两者的区别,可以参看笔者文章:
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/shenjianeng/spring-code-study
以上是关于Spring5 源码学习 ConfigurationClassPostProcessor (上)的主要内容,如果未能解决你的问题,请参考以下文章
深度解析Spring源码编译Spring源码(spring5.2.x版本)
Mybatis 源码学习-类型转换(TypeHandlerRegistry)