spring boot:v2.7.2
入口:EnvironmentPostProcessorApplicationListener
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener#onApplicationEnvironmentPreparedEvent
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
SpringApplication application = event.getSpringApplication();
for (EnvironmentPostProcessor postProcessor : getEnvironmentPostProcessors(application.getResourceLoader(),
event.getBootstrapContext())) {
postProcessor.postProcessEnvironment(environment, application);
}
}
加载系统参数,需要扫描的配置文件文件夹 :getConfigDataEnvironment
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor#postProcessEnvironment(org.springframework.core.env.ConfigurableEnvironment, org.springframework.boot.SpringApplication)
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor#getConfigDataEnvironment
默认扫描文件夹 org.springframework.boot.context.config.ConfigDataEnvironment
实例化时,添加了默认搜索地址
构造函数调用了createContributors
倒序添加
添加完成后的顺序:
optional:file:./;optional:file:./config/;optional:file:./config/*/
optional:classpath:/;optional:classpath:/config/
最终有9个配置
最终转换成org.springframework.boot.context.config.ConfigDataEnvironmentContributor,放入org.springframework.boot.context.config.ConfigDataEnvironmentContributors的root中
收集并加载配置文件:
org.springframework.boot.context.config.ConfigDataEnvironment#processAndApply
org.springframework.boot.context.config.ConfigDataEnvironmentContributors#withProcessedImports
org.springframework.boot.context.config.ConfigDataImporter#resolveAndLoad
org.springframework.boot.context.config.StandardConfigDataLocationResolver#isResolvable
返回org.springframework.boot.context.config.ConfigDataLocationResolvers#resolve(org.springframework.boot.context.config.ConfigDataLocationResolverContext, org.springframework.boot.context.config.ConfigDataLocation, org.springframework.boot.context.config.Profiles)
获取配置文件可能存在的所有路径
org.springframework.boot.context.config.StandardConfigDataLocationResolver#getReferences(org.springframework.boot.context.config.ConfigDataLocationResolverContext, org.springframework.boot.context.config.ConfigDataLocation[])
获取到地址列表后,继续判断地址有效性,最终或有效地址
有效地址就放入列表
如果没有匹配到路径,则加载空的目录(存在的文件夹目录)
加载配置文件
org.springframework.boot.context.config.ConfigDataImporter#resolveAndLoad
org.springframework.boot.context.config.StandardConfigDataLoader#load
世界用于加载的类
org.springframework.boot.env.YamlPropertySourceLoader
org.springframework.boot.env.PropertiesPropertySourceLoader
application加载:
org.springframework.boot.context.config.StandardConfigDataLocationResolver#getConfigNames
没有找到spring.config.name,就直接返回
其他的步骤就是重复bootstrap配置的加载过程