Skip to content

Commit c94bc2e

Browse files
committed
Respect spring.profiles.active in #addActiveProfile
Prior to this commit, calls to ConfigurableEnvironment#addActiveProfile would cause any active profile values provided via the "spring.profiles.active" property to be ignored. Now these two mechanisms can be used in conjunction and work as expected. Issue: SPR-9944
1 parent 74e86fe commit c94bc2e

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

spring-context/src/test/java/org/springframework/context/support/EnvironmentIntegrationTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.context.support;
1818

1919
import org.junit.Test;
20-
2120
import org.springframework.context.ApplicationContext;
2221
import org.springframework.context.ConfigurableApplicationContext;
2322
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -34,7 +33,6 @@
3433
*/
3534
public class EnvironmentIntegrationTests {
3635

37-
@SuppressWarnings("unchecked")
3836
@Test
3937
public void repro() {
4038
ConfigurableApplicationContext parent = new GenericApplicationContext();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ public void setActiveProfiles(String... profiles) {
240240
Assert.notNull(profiles, "Profile array must not be null");
241241
this.activeProfiles.clear();
242242
for (String profile : profiles) {
243-
addActiveProfile(profile);
243+
validateProfile(profile);
244+
this.activeProfiles.add(profile);
244245
}
245246
}
246247

@@ -249,9 +250,11 @@ public void addActiveProfile(String profile) {
249250
this.logger.debug(format("Activating profile '%s'", profile));
250251
}
251252
validateProfile(profile);
253+
doGetActiveProfiles();
252254
this.activeProfiles.add(profile);
253255
}
254256

257+
255258
public String[] getDefaultProfiles() {
256259
return StringUtils.toStringArray(doGetDefaultProfiles());
257260
}

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ public void addActiveProfile() {
185185
assertThat(environment.getActiveProfiles().length, is(5));
186186
}
187187

188+
@Test
189+
public void addActiveProfile_whenActiveProfilesPropertyIsAlreadySet() {
190+
ConfigurableEnvironment env = new StandardEnvironment();
191+
assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), nullValue());
192+
env.getPropertySources().addFirst(new MockPropertySource().withProperty(ACTIVE_PROFILES_PROPERTY_NAME, "p1"));
193+
assertThat(env.getProperty(ACTIVE_PROFILES_PROPERTY_NAME), equalTo("p1"));
194+
env.addActiveProfile("p2");
195+
assertThat(env.getActiveProfiles(), arrayContaining("p1", "p2"));
196+
}
197+
188198
@Test
189199
public void reservedDefaultProfile() {
190200
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));

0 commit comments

Comments
 (0)