SpringBoot配置加载的优先级

总结:

相同内容,优先级高的会覆盖优先级低的。
启动参数--spring.config.location=configFilePath/configFile > 启动参数--spring.config.additional-location=configFilePath/configFile > projectPath/config/configFile > projectPath/configFile > classpath/config/configFile > classpath/configFile

相同路径下的configFile优先级(已知):
application-profile.propertie > application.yml
spring.profile待确认

application.properties > application.yml
application-dev.yml > application.yml
bootStrap.yml > application.yml

源码:ConfigFileApplicationListener:

private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
private static final String DEFAULT_NAMES = "application";

首先在没有任何配置的情况下,会从DEFAULT_SEARCH_LOCATIONS常量列出来的位置中加载文件名为DEFAULT_NAMES(.properties或yml)的文件,默认位置包括:

  • classpath根目录(classpath:/)
  • classpath里面的config文件目录(classpath:/config/)
  • 程序运行目录(file:./)
  • 程序运行目录下的config目录(file:./config/)

 

上面说的是没有额外配置的情况,SpringBoot足够灵活可以指定配置文件搜索路径、配置文件名,在ConfigFileApplicationListener类中有个getSearchLocations方法,它主要负责获取配置搜索目录:

private Set<String> getSearchLocations() {

if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) {

 return getSearchLocations(CONFIG_LOCATION_PROPERTY);

 }

 Set<String> locations = getSearchLocations(CONFIG_ADDITIONAL_LOCATION_PROPERTY);

 locations.addAll(

 asResolvedSet(ConfigFileApplicationListener.this.searchLocations, DEFAULT_SEARCH_LOCATIONS));

 return locations;

}

它的操作步骤大致如下:

  1. 检查是否有spring.config.location属性,如果存在则直接使用它的值
  2. 从spring.config.additional-location属性中获取搜索路径
  3. 将默认搜索路径添加到搜索集合

这里就可以确定SpringBoot配置的搜索路径有两种情况:如果配置了spring.config.location则直接使用,否则使用spring.config.additional-location的属性值 + 默认搜索路径。

推荐:https://www.jb51.net/article/173537.htm

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值