目录
入口
SpringApplication.run(XXApplication.class, args);
加载spring.factories
SpringApplication#createApplicationContext
prepareContext 会注册应用bean到beanDefinitionMap
org.springframework.context.support.AbstractApplicationContext#refresh
context初始化
applicationContext
beanFactory
refresh()
1. prepareRefresh
// Prepare this context for refreshing
2. obtainFreshBeanFactory
// Tell the subclass to refresh the internal bean factory.
3. prepareBeanFactory
// Prepare the bean factory for use in this context. Configure the factory's standard context characteristics, such as the context's ClassLoader and post-processors
3.1.设置BeanClassLoader,BeanExpressionResolver,添加ResourceEditorRegistrar和ApplicationContextAwareProcessor
3.2.依赖检查和自动装配忽略的类型,*Aware类
3.3.某些类型和实例map,这些类型为BeanFactory,ResourceLoader,ApplicationEventPublisher,ApplicationContext(后三个即当前context对象实例)
3.4.添加BeanPostProcessor类,ApplicationListenerDetector
3.5.检测LoadTimeWeaver
3.6.设置默认的environment beans
4. postProcessBeanFactory
// Allows post-processing of the bean factory in context subclasses
5. invokeBeanFactoryPostProcessors
5.1 First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry
这一步是很重要的一个方法,从入口类解析这个类上的注解,包含registrar和importSelector。解析AutoConfigurationImportSelector类时从配置文件加载所有的EnableAutoConfiguration类,同时从spring-autoconfigure-metadata.properties文件获取到配置类中的规则定义,并按照conditionalOn规则加载bean定义
boolean[] match = filter.match(candidates, autoConfigurationMetadata);
// 自动配置核心方法
for (int i = 0; i < match.length; i++) {
if (!match[i]) {
skip[i] = true;
candidates[i] = null;
skipped = true;
}
}
OnClassCondition
private ConditionOutcome getOutcome(String className, ClassLoader classLoader) {
if (ClassNameFilter.MISSING.matches(className, classLoader)) {
return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnClass.class)
.didNotFind("required class").items(Style.QUOTE, className));
}
return null;
}