Merge remote-tracking branch 'origin/2.2.x'
This commit is contained in:
@@ -76,8 +76,11 @@ Thus, sibling contexts (in particular) do not need to have the same profiles or
|
||||
[[customizing-bootstrap-properties]]
|
||||
=== Changing the Location of Bootstrap Properties
|
||||
|
||||
You can specify the `bootstrap.yml` (or `.properties`) location by setting `spring.cloud.bootstrap.name` (default: `bootstrap`) or `spring.cloud.bootstrap.location` (default: empty) -- for example, in system properties.
|
||||
The `bootstrap.yml` (or `.properties`) location can be specified by setting `spring.cloud.bootstrap.name` (default: `bootstrap`), `spring.cloud.bootstrap.location` (default: empty) or `spring.cloud.bootstrap.additional-location` (default: empty) -- for example, in System properties.
|
||||
|
||||
Those properties behave like the `spring.config.*` variants with the same name.
|
||||
With `spring.cloud.bootstrap.location` the default locations are replaced and only the specified ones are used.
|
||||
To add locations to the list of default ones, `spring.cloud.bootstrap.additional-location` could be used.
|
||||
In fact, they are used to set up the bootstrap `ApplicationContext` by setting those properties in its `Environment`.
|
||||
If there is an active profile (from `spring.profiles.active` or through the `Environment` API in the context you are building), properties in that profile get loaded as well, the same as in a regular Spring Boot app -- for example, from `bootstrap-development.properties` for a `development` profile.
|
||||
|
||||
|
||||
@@ -157,6 +157,8 @@ public class BootstrapApplicationListener
|
||||
}
|
||||
String configLocation = environment
|
||||
.resolvePlaceholders("${spring.cloud.bootstrap.location:}");
|
||||
String configAdditionalLocation = environment
|
||||
.resolvePlaceholders("${spring.cloud.bootstrap.additional-location:}");
|
||||
Map<String, Object> bootstrapMap = new HashMap<>();
|
||||
bootstrapMap.put("spring.config.name", configName);
|
||||
// if an app (or test) uses spring.main.web-application-type=reactive, bootstrap
|
||||
@@ -168,6 +170,10 @@ public class BootstrapApplicationListener
|
||||
if (StringUtils.hasText(configLocation)) {
|
||||
bootstrapMap.put("spring.config.location", configLocation);
|
||||
}
|
||||
if (StringUtils.hasText(configAdditionalLocation)) {
|
||||
bootstrapMap.put("spring.config.additional-location",
|
||||
configAdditionalLocation);
|
||||
}
|
||||
bootstrapProperties.addFirst(
|
||||
new MapPropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME, bootstrapMap));
|
||||
for (PropertySource<?> source : environment.getPropertySources()) {
|
||||
|
||||
@@ -78,7 +78,7 @@ public class BootstrapConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pickupExternalBootstrapProperties() {
|
||||
public void pickupOnlyExternalBootstrapProperties() {
|
||||
String externalPropertiesPath = getExternalProperties();
|
||||
|
||||
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
|
||||
@@ -87,6 +87,25 @@ public class BootstrapConfigurationTests {
|
||||
.run();
|
||||
then(this.context.getEnvironment().getProperty("info.name"))
|
||||
.isEqualTo("externalPropertiesInfoName");
|
||||
then(this.context.getEnvironment().getProperty("info.desc")).isNull();
|
||||
then(this.context.getEnvironment().getPropertySources().contains(
|
||||
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME
|
||||
+ "-testBootstrap")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pickupAdditionalExternalBootstrapProperties() {
|
||||
String externalPropertiesPath = getExternalProperties();
|
||||
|
||||
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
|
||||
.sources(BareConfiguration.class)
|
||||
.properties("spring.cloud.bootstrap.additional-location="
|
||||
+ externalPropertiesPath)
|
||||
.run();
|
||||
then(this.context.getEnvironment().getProperty("info.name"))
|
||||
.isEqualTo("externalPropertiesInfoName");
|
||||
then(this.context.getEnvironment().getProperty("info.desc"))
|
||||
.isEqualTo("defaultPropertiesInfoDesc");
|
||||
then(this.context.getEnvironment().getPropertySources().contains(
|
||||
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME
|
||||
+ "-testBootstrap")).isTrue();
|
||||
@@ -117,7 +136,7 @@ public class BootstrapConfigurationTests {
|
||||
* @return
|
||||
*/
|
||||
private String getExternalProperties() {
|
||||
String externalPropertiesPath = "classpath:bootstrap.properties,classpath:external-properties/bootstrap.properties";
|
||||
String externalPropertiesPath = "classpath:external-properties/bootstrap.properties";
|
||||
return externalPropertiesPath;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration,org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.CompositePropertySourceConfiguration
|
||||
info.name:child
|
||||
info.desc: defaultPropertiesInfoDesc
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration
|
||||
info.name:externalPropertiesInfoName
|
||||
|
||||
Reference in New Issue
Block a user