单独使用SpringBoot,发现其中的bootstrap.properties文件不会生效。
经过源码查看原来是因为SpringBoot本身并不支持,需要Spring Cloud才能生效。
而具体的代码又在spring-cloud-context包中的org.springframework.cloud.bootstrap.BootstrapApplicationListener实现
ApplicationListener
order越小,越前
ConfigFileApplicationListener
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
// -2147483638
private int order = DEFAULT_ORDER;
BootstrapApplicationListener
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 5;
// -21474836343
private int order = DEFAULT_ORDER;
sping.factories
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.bootstrap.BootstrapApplicationListener,\
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,\
org.springframework.cloud.context.restart.RestartListener
bootstrap.properties
org.springframework.cloud.bootstrap.BootstrapApplicationListener
public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";
String configName = environment
.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
Map<String, Object> bootstrapMap = new HashMap<>();
bootstrapMap.put("spring.config.name", configName);
# 那怕配置spring.cloud.bootstrap.name,这里也是bootstarp
bootstrapProperties.addFirst(
new MapPropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME, bootstrapMap));
其他属性
String configLocation = environment
.resolvePlaceholders("${spring.cloud.bootstrap.location:}");
String configAdditionalLocation = environment
.resolvePlaceholders("${spring.cloud.bootstrap.additional-location:}");
# spring.cloud.bootstrap.enabled
ConfigurableEnvironment environment = event.getEnvironment();
if (!environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class,
true)) {
return;
}