总结:
相同内容,优先级高的会覆盖优先级低的。
启动参数--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;
}
它的操作步骤大致如下:
- 检查是否有spring.config.location属性,如果存在则直接使用它的值
- 从spring.config.additional-location属性中获取搜索路径
- 将默认搜索路径添加到搜索集合
这里就可以确定SpringBoot配置的搜索路径有两种情况:如果配置了spring.config.location则直接使用,否则使用spring.config.additional-location的属性值 + 默认搜索路径。
推荐:https://www.jb51.net/article/173537.htm