配置方式
SpringBoot指定配置文件,可以通过spring.config.location、spring.config.additional-location、或者在系统默认的位置配置
其中,spring.config.name、spring.config.location和spring.config.additional-location等配置是从environment中获取的
若没有指定spring.config.location,配置文件获取优先级为,配置文件的优先级为:
spring.config.additional-location指定的地址
file:./config/
file:./
classpath:/config/
classpath:/
原理
// Springboot默认的配置文件路径,优先级由后到前依次降低
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
// SpringBoot 添加配置文件路径的两种方式
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
private Set<String> getSearchLocations() {
//如果配置了spring.config.location,将不再获取其他的配置
if (this.environment.containsProperty(CONFIG_LOCATION_PROPERTY)) {
return getSearchLocations(CONFIG_LOCATION_PROPERTY);
}
// 添加默认路径和spring.config.additional-location路径下的配置文件
Set<String> locations = getSearchLocations(
CONFIG_ADDITIONAL_LOCATION_PROPERTY);
locations.addAll(
asResolvedSet(ConfigFileApplicationListener.this.searchLocations,
DEFAULT_SEARCH_LOCATIONS));
return locations;
}
当然不同版本的springboot设定不一样,还是要根据自己版本来