From 70129ecc07675cb41c66ac08c3819fa744ef7709 Mon Sep 17 00:00:00 2001 From: srempfer Date: Tue, 15 Jan 2019 23:40:25 +0100 Subject: [PATCH] Introduced property 'spring.cloud.bootstrap.additional-location' according to 'spring.config.additional-location' (#466) --- .../main/asciidoc/spring-cloud-commons.adoc | 4 +++- .../BootstrapApplicationListener.java | 5 ++++ .../config/BootstrapConfigurationTests.java | 24 ++++++++++++++++--- .../src/test/resources/bootstrap.properties | 1 + .../external-properties/bootstrap.properties | 1 + 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 20114c22..0bb4d945 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -79,8 +79,10 @@ Thus, sibling contexts, in particular, do not need to have the same profiles or [[customizing-bootstrap-properties]] === Changing the Location of Bootstrap Properties -The `bootstrap.yml` (or `.properties`) location can be specified 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 951fd6f7..0127dd8d 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 @@ -154,6 +154,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 @@ -165,6 +167,9 @@ 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 8cec3546..ac5d118f 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 @@ -72,7 +72,7 @@ public class BootstrapConfigurationTests { } @Test - public void pickupExternalBootstrapProperties() { + public void pickupOnlyExternalBootstrapProperties() { String externalPropertiesPath = getExternalProperties(); this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE) @@ -81,9 +81,27 @@ 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)) - .isTrue(); + .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)) + .isTrue(); } @Test @@ -111,7 +129,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 dd6946b7..a4558e34 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 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