一、在applicationContext.refresh方法中会调用invokeBeanFactoryPostProcessors(beanFactory),此方法会调用当前系统容器中所有注册的BeanDefinitionRegistryPostProcessor的对象,最终会调用ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry。
二、在ConfigurationClassPostProcessor.processConfigBeanDefinitions会循环遍历当前容器工厂内的类元注解中包含configuration,componen,ComponentScan
、 Import
和 ImportResource的对象。
三、接着会调用
ConfigurationClassParser.parse会对每个上面筛选出来的对象进行解析,如果有condition注解在类上,则会判断条件注解是否满足,不满足直接过滤,满足条件的保存到
ConfigurationClass列表中。
1.@import注解的类解析在ConfigurationClassParser.doProcessConfigurationClass。
1.1.如果注解中的类是普通类,则会递归调用ConfigurationClassParser.processConfigurationClass进行解析,并保存到parser的configurationClasses中。
// Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
// process it as an @Configuration class
this.importStack.registerImport(
currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
processConfigurationClass(candidate.asConfigClass(configClass), exclusionFilter);
1.2 如果注解中的类是实现了ImportSelector接口,则会调导入类的selectImports接口获取具体要导入的类字符串数组,然后通过asSourceClasses转换成类,再次递归调用processImports,然后就循环到1.1步骤。
注意:这里有个如果类继承了DeferredImportSelector,则为延迟加载,会放在所有普通导入BEAN的最后进行加载,并可以控制容器内所有DeferredImportSelector的加载顺序(@ORDER),这个应用场景主要用于自动配置,@springbootApplication的注解引入的@EnableAutoConfiguration注解,里面引入的那个importSelect就是延迟selector,spring先处理我们的,然后再处理自动装配的,那些自动配置类上的那些条件就可以根据我们的配置情况来判断是否应该生效。
// Candidate class is an ImportSelector -> delegate to it to determine imports
Class> candidateClass = candidate.loadClass();
ImportSelector selector = ParserStrategyUtils.instantiateClass(candidateClass, ImportSelector.class,
this.environment, this.resourceLoader, this.registry);
Predicate selectorFilter = selector.getExclusionFilter();
if (selectorFilter != null) {
exclusionFilter = exclusionFilter.or(selectorFilter);
}
if (selector instanceof DeferredImportSelector) {
this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
}
else {
String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
Collection importSourceClasses = asSourceClasses(importClassNames, exclusionFilter);
processImports(configClass, currentSourceClass, importSourceClasses, exclusionFilter, false);
}
1.3.如果注解中的导入类是ImportBeanDefinitionRegistrar类,则会将此类保存到configurationClass的importBeanDefinitionRegistrars属性列表中。
Class> candidateClass = candidate.loadClass();
ImportBeanDefinitionRegistrar registrar =
ParserStrategyUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class,
this.environment, this.resourceLoader, this.registry);
configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
2.@bean注解的类解析在ConfigurationClassParser.doProcessConfigurationClass。这个相对简单,就是解析当前解析类中所有包含@bean注解的方法,并保存到configurationClass的beanMethods集合列表中。
// Process individual @Bean methods
Set beanMethods = retrieveBeanMethodMetadata(sourceClass);
for (MethodMetadata methodMetadata : beanMethods) {
configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
}
动态数据如下:
3.@ComponentScan注解的类解析在ConfigurationClassParser.doProcessConfigurationClass,这个就是根据@COmponentScan配置的包路径列表逐个使用ComponentScanAnnotationParser.parse方法扫描包路径下的包,然后将获取的BeanDefinitionHolder集合列表再次递归调用parse(bdCand.getBeanClassName(), holder.getBeanName())进行处理,添加到parser的configurationClasses中。
Set componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
if (!componentScans.isEmpty() &&
!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
for (AnnotationAttributes componentScan : componentScans) {
// The config class is annotated with @ComponentScan -> perform the scan immediately
Set scannedBeanDefinitions =
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
// Check the set of scanned definitions for any further config classes and parse recursively if needed
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
if (bdCand == null) {
bdCand = holder.getBeanDefinition();
}
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
parse(bdCand.getBeanClassName(), holder.getBeanName());
}
}
}
}
4.@ImportResource注解的类解析在ConfigurationClassParser.doProcessConfigurationClass,这个注解用于导入Spring的配置文件,让配置文件里面的内容生效;(就是以前写的springmvc.xml、applicationContext.xml),这个解析也比较简单,只是将注解locations属性和BeanDefinitionReader的读取类作为MAP的键值保存到configurationClasse的importedResources列表属性中。
AnnotationAttributes importResource =
AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
if (importResource != null) {
String[] resources = importResource.getStringArray("locations");
Class extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
for (String resource : resources) {
String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
configClass.addImportedResource(resolvedResource, readerClass);
}
}
四、然后会调用ConfigurationClassBeanDefinitionReader.loadBeanDefinitions 循环解析每个组件对象,将新增的@bean,@import符合条件的注解对象,注入到beanfactory工厂对象中。
if (this.reader == null) {
this.reader = new ConfigurationClassBeanDefinitionReader(
registry, this.sourceExtractor, this.resourceLoader, this.environment,
this.importBeanNameGenerator, parser.getImportRegistry());
}
this.reader.loadBeanDefinitions(configClasses);
1.@bean注解的BeanDefinition对象生成实现为ConfigurationClassBeanDefinition,并设置此BEAN的工厂方法为当前@BEAN注解的方法,工厂BEAN名称为当前对象,最后会调用容器工厂将此BeanDefinition注册进去保存到beanDefinitionMap,如下图
下面是配置类的代码:
package com.tpw.newday.service.people;
import com.tpw.newday.bean.PeopleBean;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
/**
* newday
*
*
* @author : lipengyao
* @date : 2021-07-01 15:38:01
**/
@Configuration
@Conditional(BeanFactoryCondition.class)
public class MsgConf {
@Bean(name = {"msgSpringBean"})
@Conditional(SpringCondition.class)
public PeopleBean msgSpringBean(){
System.out.println(" MsgConf -->msgSpringBean: " );
return new PeopleBean("msgSpringBean");
}
@Bean(name = {"msgOriginFactoryBean"})
@Conditional(BeanFactoryCondition.class)
public PeopleBean msgOriginFactoryBean(){
System.out.println(" MsgConf -->msgOriginFactoryBean: " );
return new PeopleBean("msgOriginFactoryBean");
}
}
2.@importResource注解的BeanDefinition对象会先根据RESOURCE的配置文件路径生成XmlBeanDefinitionReader(register容器工厂作为参数),然后调用
reader.loadBeanDefinitions(resource),将配置中的所有BEAN实例注册到容器工厂中。
private void loadBeanDefinitionsFromImportedResources(
Map> importedResources) {
Map, BeanDefinitionReader> readerInstanceCache = new HashMap<>();
importedResources.forEach((resource, readerClass) -> {
// Default reader selection necessary?
if (BeanDefinitionReader.class == readerClass) {
if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
// When clearly asking for Groovy, that's what they'll get...
readerClass = GroovyBeanDefinitionReader.class;
}
else {
// Primarily ".xml" files but for any other extension as well
readerClass = XmlBeanDefinitionReader.class;
}
}
BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
if (reader == null) {
try {
// Instantiate the specified BeanDefinitionReader
reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
// Delegate the current ResourceLoader to it if possible
if (reader instanceof AbstractBeanDefinitionReader) {
AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
abdr.setResourceLoader(this.resourceLoader);
abdr.setEnvironment(this.environment);
}
readerInstanceCache.put(readerClass, reader);
}
catch (Throwable ex) {
throw new IllegalStateException(
"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
}
}
// TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
reader.loadBeanDefinitions(resource);
});
}
3.@import中实现了ImportBeanDefinitionRegistrar接口,则会把configurationClass的importBeanDefinitionRegistrars属性列表所有导入BEAN注册表循环注册复制到容器工厂注册表中。这个比较简单。
private void loadBeanDefinitionsFromRegistrars(Map registrars) {
registrars.forEach((registrar, metadata) ->
registrar.registerBeanDefinitions(metadata, this.registry, this.importBeanNameGenerator));
}
五、循环遍历当前新解析出来的对象,又回到第二步的逻辑,判断新对象中是否包含configuration,componen,ComponentScan
、 Import
和 ImportResource的注解,如果有,则添加到新的candidates中,再次循环,解析,加载数据。
// Parse each @Configuration class
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment,
this.resourceLoader, this.componentScanBeanNameGenerator, registry);
Set candidates = new LinkedHashSet<>(configCandidates);
Set alreadyParsed = new HashSet<>(configCandidates.size());
do {
parser.parse(candidates);
parser.validate();
Set 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());
}
this.reader.loadBeanDefinitions(configClasses);
alreadyParsed.addAll(configClasses);
candidates.clear();
if (registry.getBeanDefinitionCount() > candidateNames.length) {
String[] newCandidateNames = registry.getBeanDefinitionNames();
Set oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
Set 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());
六、前面所有的工作都是注册
BeanDefinition到容器工厂,具体的BEAN真正实例化创建都是在最后的applicationContext.
finishBeanFactoryInitialization中去初始化非延迟的单例BEAN,这个前面文章已经讲过,不再复核。