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
This commit is contained in:
parent
c0eeb8bacd
commit
76bf72c9f8
|
|
@ -188,8 +188,16 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActiveProfiles(String... profiles) {
|
public void setActiveProfiles(String... profiles) {
|
||||||
|
Assert.notNull(profiles, "Profile array must not be null");
|
||||||
this.activeProfiles.clear();
|
this.activeProfiles.clear();
|
||||||
this.activeProfiles.addAll(Arrays.asList(profiles));
|
for (String profile : profiles) {
|
||||||
|
this.addActiveProfile(profile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addActiveProfile(String profile) {
|
||||||
|
this.validateProfile(profile);
|
||||||
|
this.activeProfiles.add(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getDefaultProfiles() {
|
public String[] getDefaultProfiles() {
|
||||||
|
|
@ -226,6 +234,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
* @see #getReservedDefaultProfiles()
|
* @see #getReservedDefaultProfiles()
|
||||||
*/
|
*/
|
||||||
public void setDefaultProfiles(String... profiles) {
|
public void setDefaultProfiles(String... profiles) {
|
||||||
|
Assert.notNull(profiles, "Profile array must not be null");
|
||||||
this.defaultProfiles.clear();
|
this.defaultProfiles.clear();
|
||||||
for (String profile : profiles) {
|
for (String profile : profiles) {
|
||||||
this.validateProfile(profile);
|
this.validateProfile(profile);
|
||||||
|
|
@ -255,6 +264,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
* <p>Subclasses may override to impose further restrictions on profile syntax.
|
* <p>Subclasses may override to impose further restrictions on profile syntax.
|
||||||
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
|
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
|
||||||
* @see #acceptsProfiles
|
* @see #acceptsProfiles
|
||||||
|
* @see #addActiveProfile
|
||||||
* @see #setDefaultProfiles
|
* @see #setDefaultProfiles
|
||||||
*/
|
*/
|
||||||
protected void validateProfile(String profile) {
|
protected void validateProfile(String profile) {
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,9 @@ package org.springframework.core.env;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration interface to be implemented by most if not all {@link Environment
|
* Configuration interface to be implemented by most if not all {@link Environment} types.
|
||||||
* Environments}. Provides facilities for setting active and default profiles as well
|
* Provides facilities for setting active and default profiles as well
|
||||||
* as accessing the {@linkplain #getPropertySources() property sources}.
|
* as accessing underlying {@linkplain #getPropertySources() property sources}.
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
|
|
@ -35,8 +35,10 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
|
||||||
* evaluated during container bootstrap to determine whether bean definitions
|
* evaluated during container bootstrap to determine whether bean definitions
|
||||||
* should be registered with the container.
|
* should be registered with the container.
|
||||||
* <p>Any existing active profiles will be replaced with the given arguments; call
|
* <p>Any existing active profiles will be replaced with the given arguments; call
|
||||||
* with zero arguments to clear the current set of active profiles.
|
* with zero arguments to clear the current set of active profiles. Use
|
||||||
|
* {@link #addActiveProfile} to add a profile while preserving the existing set.
|
||||||
*
|
*
|
||||||
|
* @see #addActiveProfile
|
||||||
* @see #setDefaultProfiles
|
* @see #setDefaultProfiles
|
||||||
* @see org.springframework.context.annotation.Profile
|
* @see org.springframework.context.annotation.Profile
|
||||||
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||||
|
|
@ -44,6 +46,13 @@ public interface ConfigurableEnvironment extends Environment, ConfigurableProper
|
||||||
*/
|
*/
|
||||||
void setActiveProfiles(String... profiles);
|
void setActiveProfiles(String... profiles);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a profile to the current set of active profiles.
|
||||||
|
* @see #setActiveProfiles
|
||||||
|
* @throws IllegalArgumentException if the profile is null, empty or whitespace-only
|
||||||
|
*/
|
||||||
|
void addActiveProfile(String profile);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the set of profiles to be made active by default if no other profiles
|
* Specify the set of profiles to be made active by default if no other profiles
|
||||||
* are explicitly made active through {@link #setActiveProfiles}.
|
* are explicitly made active through {@link #setActiveProfiles}.
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,21 @@ public class EnvironmentTests {
|
||||||
environment.setDefaultProfiles("");
|
environment.setDefaultProfiles("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addActiveProfile() {
|
||||||
|
assertThat(environment.getActiveProfiles().length, is(0));
|
||||||
|
environment.setActiveProfiles("local", "embedded");
|
||||||
|
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("local", "embedded"));
|
||||||
|
assertThat(environment.getActiveProfiles().length, is(2));
|
||||||
|
environment.addActiveProfile("p1");
|
||||||
|
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p1"));
|
||||||
|
assertThat(environment.getActiveProfiles().length, is(3));
|
||||||
|
environment.addActiveProfile("p2");
|
||||||
|
environment.addActiveProfile("p3");
|
||||||
|
assertThat(Arrays.asList(environment.getActiveProfiles()), hasItems("p2", "p3"));
|
||||||
|
assertThat(environment.getActiveProfiles().length, is(5));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void reservedDefaultProfile() {
|
public void reservedDefaultProfile() {
|
||||||
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));
|
assertThat(environment.getDefaultProfiles(), equalTo(new String[]{RESERVED_DEFAULT_PROFILE_NAME}));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue