From 9b2d24753703e235191909773e83e625dc519f6b Mon Sep 17 00:00:00 2001 From: Jinghao Song <8175420+leovx@users.noreply.github.com> Date: Thu, 19 Oct 2023 05:04:32 +0800 Subject: [PATCH 1/2] 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 --- .../PropertySourceBootstrapConfiguration.java | 6 ++++++ .../config/BootstrapConfigurationTests.java | 17 +++++++++++++++++ .../src/test/resources/application.properties | 1 + 3 files changed, 24 insertions(+) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index cc3e94344..4a7e6ce85 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -285,6 +285,12 @@ private Set addIncludedProfilesTo(Set profiles, PropertySource addActiveProfilesTo(List 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); } 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 90209b2d1..3cd8fc15a 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 @@ -654,6 +654,23 @@ private void nonEnumerablePropertySourceWorks(String... properties) { 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 { diff --git a/spring-cloud-context/src/test/resources/application.properties b/spring-cloud-context/src/test/resources/application.properties index d92f2ea44..cfb461b5d 100644 --- a/spring-cloud-context/src/test/resources/application.properties +++ b/spring-cloud-context/src/test/resources/application.properties @@ -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 From 1dbe6c0c39c83bbeafe781490cb774cdb4f285fb Mon Sep 17 00:00:00 2001 From: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:56:11 -0400 Subject: [PATCH 2/2] Fix bad merge --- .../config/PropertySourceBootstrapConfiguration.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index b75794db4..e2d44aa84 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -38,7 +38,6 @@ import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.cloud.bootstrap.BootstrapApplicationListener; -import org.springframework.cloud.bootstrap.BootstrapConfigFileApplicationListener; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.logging.LoggingRebinder; import org.springframework.context.ApplicationContextInitializer; @@ -302,9 +301,7 @@ private > T addProfilesTo(T profiles, PropertySourc } } else { - Collections.addAll(profiles, getProfilesForValue( - propertySource.getProperty(BootstrapConfigFileApplicationListener.INCLUDE_PROFILES_PROPERTY), - environment)); + Collections.addAll(profiles, getProfilesForValue(propertySource.getProperty(property), environment)); } return profiles; }