Skip to content

Commit 76bf72c

Browse files
committed
Introduce ConfigurableEnvironment#addActiveProfile
Provides a convenient mechanism for activating an additional profile while preserving those that are already active, as opposed to calling #setActiveProfiles with the contents of #getActiveProfiles plus the new profile. Issue: SPR-8548
1 parent c0eeb8b commit 76bf72c

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,16 @@ protected Set<String> doGetActiveProfiles() {
188188
}
189189

190190
public void setActiveProfiles(String... profiles) {
191+
Assert.notNull(profiles, "Profile array must not be null");
191192
this.activeProfiles.clear();
192-
this.activeProfiles.addAll(Arrays.asList(profiles));
193+
for (String profile : profiles) {
194+
this.addActiveProfile(profile);
195+
}
196+
}
197+
198+
public void addActiveProfile(String profile) {
199+
this.validateProfile(profile);
200+
this.activeProfiles.add(profile);
193201
}
194202

195203
public String[] getDefaultProfiles() {
@@ -226,6 +234,7 @@ protected Set<String> doGetDefaultProfiles() {
226234
* @see #getReservedDefaultProfiles()
227235
*/
228236
public void setDefaultProfiles(String... profiles) {
237+
Assert.notNull(profiles, "Profile array must not be null");
229238
this.defaultProfiles.clear();
230239
for (String profile : profiles) {
231240
this.validateProfile(profile);
@@ -255,6 +264,7 @@ public boolean acceptsProfiles(String... profiles) {
255264
* <p>Subclasses may override to impose further restrictions on profile syntax.
256265
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
257266
* @see #acceptsProfiles
267+
* @see #addActiveProfile
258268
* @see #setDefaultProfiles
259269
*/
260270
protected void validateProfile(String profile) {

org.springframework.core/src/main/java/org/springframework/core/env/ConfigurableEnvironment.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import java.util.Map;
2020

2121
/**
22-
* Configuration interface to be implemented by most if not all {@link Environment
23-
* Environments}. Provides facilities for setting active and default profiles as well
24-
* as accessing the {@linkplain #getPropertySources() property sources}.
22+
* Configuration interface to be implemented by most if not all {@link Environment} types.
23+
* Provides facilities for setting active and default profiles as well
24+
* as accessing underlying {@linkplain #getPropertySources() property sources}.
2525
*
2626
* @author Chris Beams
2727
* @since 3.1
@@ -35,15 +35,24 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
3535
* evaluated during container bootstrap to determine whether bean definitions
3636
* should be registered with the container.
3737
* <p>Any existing active profiles will be replaced with the given arguments; call
38-
* with zero arguments to clear the current set of active profiles.
38+
* with zero arguments to clear the current set of active profiles. Use
39+
* {@link #addActiveProfile} to add a profile while preserving the existing set.
3940
*
41+
* @see #addActiveProfile
4042
* @see #setDefaultProfiles
4143
* @see org.springframework.context.annotation.Profile
4244
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
4345
* @throws IllegalArgumentException if any profile is null, empty or whitespace-only
4446
*/
4547
void setActiveProfiles(String... profiles);
4648

49+
/**
50+
* Add a profile to the current set of active profiles.
51+
* @see #setActiveProfiles
52+
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
53+
*/
54+
void addActiveProfile(String profile);
55+
4756
/**
4857
* Specify the set of profiles to be made active by default if no other profiles
4958
* are explicitly made active through {@link #setActiveProfiles}.

org.springframework.core/src/test/java/org/springframework/core/env/EnvironmentTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ public void setDefaultProfiles_withEmptyProfile() {
120120
environment.setDefaultProfiles("");
121121
}
122122

123+
@Test
124+
public void addActiveProfile() {
125+
assertThat(environment.getActiveProfiles().length, is(0));
126+
environment.setActiveProfiles("local", "embedded");
127+
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("local", "embedded"));
128+
assertThat(environment.getActiveProfiles().length, is(2));
129+
environment.addActiveProfile("p1");
130+
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p1"));
131+
assertThat(environment.getActiveProfiles().length, is(3));
132+
environment.addActiveProfile("p2");
133+
environment.addActiveProfile("p3");
134+
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p2", "p3"));
135+
assertThat(environment.getActiveProfiles().length, is(5));
136+
}
137+
123138
@Test
124139
public void reservedDefaultProfile() {
125140
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));

0 commit comments

Comments
 (0)