diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 3ebf1287..7a435660 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -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. diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java index 4b172283..033f3508 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java @@ -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 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()) { diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java index 2d6597cc..2ab89dad 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java @@ -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; } diff --git a/spring-cloud-context/src/test/resources/bootstrap.properties b/spring-cloud-context/src/test/resources/bootstrap.properties index 3bac76f8..46958535 100644 --- a/spring-cloud-context/src/test/resources/bootstrap.properties +++ b/spring-cloud-context/src/test/resources/bootstrap.properties @@ -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 diff --git a/spring-cloud-context/src/test/resources/external-properties/bootstrap.properties b/spring-cloud-context/src/test/resources/external-properties/bootstrap.properties index 49cbc6ac..ad04aa1c 100644 --- a/spring-cloud-context/src/test/resources/external-properties/bootstrap.properties +++ b/spring-cloud-context/src/test/resources/external-properties/bootstrap.properties @@ -1 +1,2 @@ +spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration info.name:externalPropertiesInfoName