diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 6d45f157df..872f51fd73 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -358,8 +358,9 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, // The default profile for these purposes is represented as null. We add it // first so that it is processed first and has lowest priority. this.profiles.add(null); - Set activatedViaProperty = getProfilesFromProperty(ACTIVE_PROFILES_PROPERTY); - Set includedViaProperty = getProfilesFromProperty(INCLUDE_PROFILES_PROPERTY); + Binder binder = Binder.get(this.environment); + Set activatedViaProperty = getProfiles(binder, ACTIVE_PROFILES_PROPERTY); + Set includedViaProperty = getProfiles(binder, INCLUDE_PROFILES_PROPERTY); List otherActiveProfiles = getOtherActiveProfiles(activatedViaProperty, includedViaProperty); this.profiles.addAll(otherActiveProfiles); // Any pre-existing active profiles set via property sources (e.g. @@ -374,15 +375,6 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } } - private Set getProfilesFromProperty(String profilesProperty) { - if (!this.environment.containsProperty(profilesProperty)) { - return Collections.emptySet(); - } - Binder binder = Binder.get(this.environment); - Set profiles = getProfiles(binder, profilesProperty); - return new LinkedHashSet<>(profiles); - } - private List getOtherActiveProfiles(Set activatedViaProperty, Set includedViaProperty) { return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter( @@ -595,8 +587,10 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, return loaded.stream().map((propertySource) -> { Binder binder = new Binder(ConfigurationPropertySources.from(propertySource), this.placeholdersResolver); - return new Document(propertySource, binder.bind("spring.profiles", STRING_ARRAY).orElse(null), - getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, INCLUDE_PROFILES_PROPERTY)); + String[] profiles = binder.bind("spring.profiles", STRING_ARRAY).orElse(null); + Set activeProfiles = getProfiles(binder, ACTIVE_PROFILES_PROPERTY); + Set includeProfiles = getProfiles(binder, INCLUDE_PROFILES_PROPERTY); + return new Document(propertySource, profiles, activeProfiles, includeProfiles); }).collect(Collectors.toList()); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 0816a41e0b..6a5da9e87c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -471,6 +471,16 @@ class ConfigFileApplicationListenerTests { validateProfilePreference(output, null, "other", "dev"); } + @Test + void profilesAddedToEnvironmentAndViaPropertyWithBracketNotation(CapturedOutput output) { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.profiles.active[0]=dev", + "spring.profiles.active[1]=other"); + this.initializer.postProcessEnvironment(this.environment, this.application); + assertThat(this.environment.getActiveProfiles()).contains("dev", "other"); + assertThat(this.environment.getProperty("my.property")).isEqualTo("fromotherpropertiesfile"); + validateProfilePreference(output, null, "dev", "other"); + } + @Test void postProcessorsAreOrderedCorrectly() { TestConfigFileApplicationListener testListener = new TestConfigFileApplicationListener();