Skip to content

Commit de89154

Browse files
committed
Allow square bracket notation profiles properties
Update `ConfigFileApplicationListener` so that `spring.profiles.active` and `spring.profiles.include` can use the square bracket list notation. Prior to this commit, only comma-separated lists could be used for those values. Closes gh-21006
1 parent e3b8478 commit de89154

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,9 @@ private void initializeProfiles() {
358358
// The default profile for these purposes is represented as null. We add it
359359
// first so that it is processed first and has lowest priority.
360360
this.profiles.add(null);
361-
Set<Profile> activatedViaProperty = getProfilesFromProperty(ACTIVE_PROFILES_PROPERTY);
362-
Set<Profile> includedViaProperty = getProfilesFromProperty(INCLUDE_PROFILES_PROPERTY);
361+
Binder binder = Binder.get(this.environment);
362+
Set<Profile> activatedViaProperty = getProfiles(binder, ACTIVE_PROFILES_PROPERTY);
363+
Set<Profile> includedViaProperty = getProfiles(binder, INCLUDE_PROFILES_PROPERTY);
363364
List<Profile> otherActiveProfiles = getOtherActiveProfiles(activatedViaProperty, includedViaProperty);
364365
this.profiles.addAll(otherActiveProfiles);
365366
// Any pre-existing active profiles set via property sources (e.g.
@@ -374,15 +375,6 @@ private void initializeProfiles() {
374375
}
375376
}
376377

377-
private Set<Profile> getProfilesFromProperty(String profilesProperty) {
378-
if (!this.environment.containsProperty(profilesProperty)) {
379-
return Collections.emptySet();
380-
}
381-
Binder binder = Binder.get(this.environment);
382-
Set<Profile> profiles = getProfiles(binder, profilesProperty);
383-
return new LinkedHashSet<>(profiles);
384-
}
385-
386378
private List<Profile> getOtherActiveProfiles(Set<Profile> activatedViaProperty,
387379
Set<Profile> includedViaProperty) {
388380
return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter(
@@ -595,8 +587,10 @@ private List<Document> asDocuments(List<PropertySource<?>> loaded) {
595587
return loaded.stream().map((propertySource) -> {
596588
Binder binder = new Binder(ConfigurationPropertySources.from(propertySource),
597589
this.placeholdersResolver);
598-
return new Document(propertySource, binder.bind("spring.profiles", STRING_ARRAY).orElse(null),
599-
getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, INCLUDE_PROFILES_PROPERTY));
590+
String[] profiles = binder.bind("spring.profiles", STRING_ARRAY).orElse(null);
591+
Set<Profile> activeProfiles = getProfiles(binder, ACTIVE_PROFILES_PROPERTY);
592+
Set<Profile> includeProfiles = getProfiles(binder, INCLUDE_PROFILES_PROPERTY);
593+
return new Document(propertySource, profiles, activeProfiles, includeProfiles);
600594
}).collect(Collectors.toList());
601595
}
602596

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,16 @@ void profilesAddedToEnvironmentAndViaPropertyDuplicateEnvironmentWins(CapturedOu
471471
validateProfilePreference(output, null, "other", "dev");
472472
}
473473

474+
@Test
475+
void profilesAddedToEnvironmentAndViaPropertyWithBracketNotation(CapturedOutput output) {
476+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.profiles.active[0]=dev",
477+
"spring.profiles.active[1]=other");
478+
this.initializer.postProcessEnvironment(this.environment, this.application);
479+
assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
480+
assertThat(this.environment.getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
481+
validateProfilePreference(output, null, "dev", "other");
482+
}
483+
474484
@Test
475485
void postProcessorsAreOrderedCorrectly() {
476486
TestConfigFileApplicationListener testListener = new TestConfigFileApplicationListener();

0 commit comments

Comments
 (0)