Browse Source

Fix #1285 Spring Cloud activates unexpected profiles when some conditions met (#1286)

* Fix #1285 Spring Cloud activates unexpected profiles when some conditions met

* Fix code style
3.1.x
Jinghao Song 11 months ago committed by GitHub
parent
commit
9b2d247537
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java
  2. 17
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java
  3. 1
      spring-cloud-context/src/test/resources/application.properties

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

@ -285,6 +285,12 @@ public class PropertySourceBootstrapConfiguration implements ApplicationListener @@ -285,6 +285,12 @@ public class PropertySourceBootstrapConfiguration implements ApplicationListener
private List<String> addActiveProfilesTo(List<String> profiles, PropertySource<?> propertySource,
ConfigurableEnvironment environment) {
// According to Spring Boot, "spring.profiles.active" should have priority,
// only value from property source with the highest priority wins.
// Once settled, ignore others
if (!profiles.isEmpty()) {
return profiles;
}
return addProfilesTo(profiles, propertySource, AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, environment);
}

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

@ -654,6 +654,23 @@ public class BootstrapConfigurationTests { @@ -654,6 +654,23 @@ public class BootstrapConfigurationTests {
then(this.context.getEnvironment().getProperty("foo")).isEqualTo("bar");
}
@Test
void activeAndIncludeProfileFromBootstrapPropertySource_WhenMultiplePlacesHaveActiveProfileProperties_ShouldOnlyAcceptTheTopPriority() {
String[] properties = new String[] { "spring.config.use-legacy-processing=true" };
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE).properties(properties)
.sources(BareConfiguration.class).run("--spring.profiles.active=prod,secure");
then(this.context.getEnvironment().acceptsProfiles("prod", "secure")).isTrue();
// active profile from property sources with lower priority should not be included
then(this.context.getEnvironment().acceptsProfiles("local")).isFalse();
then(this.context.getEnvironment().getActiveProfiles()).contains("prod", "secure");
then(this.context.getEnvironment().getActiveProfiles()).doesNotContain("local");
// check if active profile value could possibly exist in other property sources
// with lower priority
then(this.context.getEnvironment().getPropertySources().stream()
.map(p -> p.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)).anyMatch("local"::equals))
.isTrue();
}
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
protected static class BareConfiguration {

1
spring-cloud-context/src/test/resources/application.properties

@ -8,3 +8,4 @@ debug:true @@ -8,3 +8,4 @@ debug:true
logging.level.org.hibernate=ERROR
logging.level.com.zaxxer.hikari=ERROR
myplaceholder=${vcap.services.myvcap.myvar}
spring.profiles.active=local

Loading…
Cancel
Save