Browse Source

merge 2.1.x into 2.2.x

pull/680/head
Ryan Baxter 5 years ago
parent
commit
cb0a693cc9
  1. 5
      docs/src/main/asciidoc/spring-cloud-commons.adoc
  2. 6
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java
  3. 23
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java
  4. 1
      spring-cloud-context/src/test/resources/bootstrap.properties
  5. 1
      spring-cloud-context/src/test/resources/external-properties/bootstrap.properties

5
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 @@ -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.

6
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java

@ -157,6 +157,8 @@ public class BootstrapApplicationListener @@ -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 @@ -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()) {

23
spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

@ -78,7 +78,7 @@ public class BootstrapConfigurationTests { @@ -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 { @@ -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 { @@ -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
spring-cloud-context/src/test/resources/bootstrap.properties

@ -1,2 +1,3 @@ @@ -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
spring-cloud-context/src/test/resources/external-properties/bootstrap.properties

@ -1 +1,2 @@ @@ -1 +1,2 @@
spring.main.sources:org.springframework.cloud.bootstrap.config.BootstrapConfigurationTests.PropertySourceConfiguration
info.name:externalPropertiesInfoName

Loading…
Cancel
Save