Skip to content

Commit

Permalink
Merge branch '4.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Oct 19, 2023
2 parents 2331598 + 1dbe6c0 commit f51cc1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -284,6 +283,12 @@ private Set<String> addIncludedProfilesTo(Set<String> profiles, PropertySource<?

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);
}

Expand All @@ -296,9 +301,7 @@ private <T extends Collection<String>> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f51cc1f

Please sign in to comment.