转载

Spring5源码分析(五)ConfigurationClassPostProcessor

Spring5源码分析(四)refresh方法 中讲到了 refresh(); 方法中的 invokeBeanFactoryPostProcessors(beanFactory); 方法。在该方法中,最关键的事情是执行了 org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry 方法。执行完该方法之后,程序中的Bean就被注册到了Spring容器中了。现在就来分析一下 ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry 方法

源码学习笔记GITHUB仓库地址

ConfigurationClassPostProcessor

Spring5源码分析(五)ConfigurationClassPostProcessor

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

通过 Spring5源码分析(四)refresh方法 ,我们可以知道, 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,然后循环这个数组,判断其是否为配置类。

Spring5源码分析(五)ConfigurationClassPostProcessor

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

在Spring中存在两种ConfigurationClass,一种是 FullConfigurationClass 另一种是 LiteConfigurationClassConfigurationClassUtils#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 变量中存放的配置类是否为空,如果不为空,则对其进行排序。

Spring5源码分析(五)ConfigurationClassPostProcessor

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

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

Spring5源码分析(五)ConfigurationClassPostProcessor

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

Spring5源码分析(五)ConfigurationClassPostProcessor

解析完配置类之后,紧接着又执行了 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 接口的使用,可以点击查看: Spring中@Import的各种用法以及ImportAware接口

原文  https://juejin.im/post/5d69e26cf265da03f33369f3
正文到此结束
Loading...