spring boot 加载配置文件

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配置的加载过程

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值