Eliminate reserved 'default' profile (SPR-7778)
There is no longer a reserved default profile named 'default'. Rather,
users must explicitly specify a default profile or profiles via
ConfigurableEnvironment.setDefaultProfiles(String...)
- or -
spring.profile.default="pD1,pD2"
Per above, the setDefaultProfile(String) method now accepts a variable
number of profile names (one or more). This is symmetrical with the
existing setActiveProfiles(String...) method.
A typical scenario might involve setting both a default profile as a
servlet context property in web.xml and then setting an active profile
when deploying to production.
This commit is contained in:
@@ -40,6 +40,7 @@ import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,32 +52,23 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
|
||||
public static final String ACTIVE_PROFILES_PROPERTY_NAME = "spring.profile.active";
|
||||
|
||||
public static final String DEFAULT_PROFILE_PROPERTY_NAME = "spring.profile.default";
|
||||
|
||||
/**
|
||||
* Default name of the default profile. Override with
|
||||
* {@link #setDefaultProfile(String)}.
|
||||
*
|
||||
* @see #setDefaultProfile(String)
|
||||
*/
|
||||
public static final String DEFAULT_PROFILE_NAME = "default";
|
||||
public static final String DEFAULT_PROFILES_PROPERTY_NAME = "spring.profile.default";
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private Set<String> activeProfiles = new LinkedHashSet<String>();
|
||||
private Set<String> defaultProfiles = new LinkedHashSet<String>();
|
||||
|
||||
private LinkedList<PropertySource<?>> propertySources = new LinkedList<PropertySource<?>>();
|
||||
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||
|
||||
private final PropertyPlaceholderHelper nonStrictHelper =
|
||||
new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true);
|
||||
|
||||
private final PropertyPlaceholderHelper strictHelper =
|
||||
new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, false);
|
||||
|
||||
private Set<String> activeProfiles = new LinkedHashSet<String>();
|
||||
private LinkedList<PropertySource<?>> propertySources = new LinkedList<PropertySource<?>>();
|
||||
private ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||
|
||||
private boolean explicitlySetProfiles;
|
||||
|
||||
private String defaultProfile = DEFAULT_PROFILE_NAME;
|
||||
|
||||
|
||||
public void addPropertySource(PropertySource<?> propertySource) {
|
||||
@@ -187,28 +179,35 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
}
|
||||
|
||||
public Set<String> getActiveProfiles() {
|
||||
doGetProfiles();
|
||||
if (this.activeProfiles.isEmpty()) {
|
||||
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
|
||||
if (StringUtils.hasText(profiles)) {
|
||||
this.activeProfiles = commaDelimitedListToSet(trimAllWhitespace(profiles));
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableSet(activeProfiles);
|
||||
}
|
||||
|
||||
private void doGetProfiles() {
|
||||
if (explicitlySetProfiles)
|
||||
return;
|
||||
|
||||
String profiles = getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
|
||||
if (profiles == null || profiles.equals("")) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.activeProfiles = commaDelimitedListToSet(trimAllWhitespace(profiles));
|
||||
}
|
||||
|
||||
public void setActiveProfiles(String... profiles) {
|
||||
explicitlySetProfiles = true;
|
||||
this.activeProfiles.clear();
|
||||
this.activeProfiles.addAll(Arrays.asList(profiles));
|
||||
}
|
||||
|
||||
public Set<String> getDefaultProfiles() {
|
||||
if (this.defaultProfiles.isEmpty()) {
|
||||
String profiles = getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
|
||||
if (StringUtils.hasText(profiles)) {
|
||||
this.defaultProfiles = commaDelimitedListToSet(profiles);
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableSet(this.defaultProfiles);
|
||||
}
|
||||
|
||||
public void setDefaultProfiles(String... profiles) {
|
||||
this.defaultProfiles.clear();
|
||||
this.defaultProfiles.addAll(Arrays.asList(profiles));
|
||||
}
|
||||
|
||||
public Map<String, String> getSystemEnvironment() {
|
||||
Map<String,String> systemEnvironment;
|
||||
try {
|
||||
@@ -282,9 +281,10 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
public boolean acceptsProfiles(String[] specifiedProfiles) {
|
||||
boolean activeProfileFound = false;
|
||||
Set<String> activeProfiles = this.getActiveProfiles();
|
||||
Set<String> defaultProfiles = this.getDefaultProfiles();
|
||||
for (String profile : specifiedProfiles) {
|
||||
if (activeProfiles.contains(profile)
|
||||
|| (activeProfiles.isEmpty() && profile.equals(this.getDefaultProfile()))) {
|
||||
|| (activeProfiles.isEmpty() && defaultProfiles.contains(profile))) {
|
||||
activeProfileFound = true;
|
||||
break;
|
||||
}
|
||||
@@ -292,18 +292,6 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
return activeProfileFound;
|
||||
}
|
||||
|
||||
public String getDefaultProfile() {
|
||||
String defaultProfileProperty = getProperty(DEFAULT_PROFILE_PROPERTY_NAME);
|
||||
if (defaultProfileProperty != null) {
|
||||
return defaultProfileProperty;
|
||||
}
|
||||
return defaultProfile;
|
||||
}
|
||||
|
||||
public void setDefaultProfile(String defaultProfile) {
|
||||
this.defaultProfile = defaultProfile;
|
||||
}
|
||||
|
||||
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
|
||||
return helper.replacePlaceholders(text, new PlaceholderResolver() {
|
||||
public String resolvePlaceholder(String placeholderName) {
|
||||
@@ -314,8 +302,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + " [activeProfiles=" + activeProfiles
|
||||
+ ", propertySources=" + propertySources + "]";
|
||||
return String.format("%s [activeProfiles=%s, defaultProfiles=%s, propertySources=%s]",
|
||||
getClass().getSimpleName(), activeProfiles, defaultProfiles, propertySources);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,10 +30,8 @@ public interface ConfigurableEnvironment extends Environment, PropertySourceAggr
|
||||
void setActiveProfiles(String... profiles);
|
||||
|
||||
/**
|
||||
* Set the default profile name to be used instead of 'default'
|
||||
*
|
||||
* @param defaultProfile
|
||||
* TODO SPR-7508: document
|
||||
*/
|
||||
void setDefaultProfile(String defaultProfile);
|
||||
void setDefaultProfiles(String... profiles);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public interface Environment {
|
||||
/**
|
||||
* TODO SPR-7508: document
|
||||
*/
|
||||
String getDefaultProfile();
|
||||
Set<String> getDefaultProfiles();
|
||||
|
||||
/**
|
||||
* TODO SPR-7508: document
|
||||
|
||||
@@ -25,12 +25,12 @@ import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.matchers.JUnitMatchers.hasItem;
|
||||
import static org.junit.matchers.JUnitMatchers.hasItems;
|
||||
import static org.springframework.core.env.AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME;
|
||||
import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILE_NAME;
|
||||
import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILE_PROPERTY_NAME;
|
||||
import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;
|
||||
import static org.springframework.core.env.DefaultEnvironmentTests.CollectionMatchers.isEmpty;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -239,26 +239,28 @@ public class DefaultEnvironmentTests {
|
||||
@Test
|
||||
public void systemPropertiesResoloutionOfMultipleProfiles() {
|
||||
assertThat(environment.getActiveProfiles(), isEmpty());
|
||||
|
||||
System.setProperty(ACTIVE_PROFILES_PROPERTY_NAME, "foo,bar");
|
||||
assertThat(environment.getActiveProfiles(), hasItems("foo", "bar"));
|
||||
System.getProperties().remove(ACTIVE_PROFILES_PROPERTY_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void systemPropertiesResolutionOfMulitpleProfiles_withWhitespace() {
|
||||
assertThat(environment.getActiveProfiles(), isEmpty());
|
||||
System.setProperty(ACTIVE_PROFILES_PROPERTY_NAME, " bar , baz "); // notice whitespace
|
||||
assertThat(environment.getActiveProfiles(), not(hasItems("foo", "bar")));
|
||||
assertThat(environment.getActiveProfiles(), hasItems("bar", "baz"));
|
||||
|
||||
System.getProperties().remove(ACTIVE_PROFILES_PROPERTY_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void environmentResolutionOfDefaultSpringProfileProperty_noneSet() {
|
||||
assertThat(environment.getDefaultProfile(), equalTo(DEFAULT_PROFILE_NAME));
|
||||
assertThat(environment.getDefaultProfiles(), isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void environmentResolutionOfDefaultSpringProfileProperty_isSet() {
|
||||
testProperties.setProperty(DEFAULT_PROFILE_PROPERTY_NAME, "custom-default");
|
||||
assertThat(environment.getDefaultProfile(), equalTo("custom-default"));
|
||||
testProperties.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "custom-default");
|
||||
assertTrue(environment.getDefaultProfiles().contains("custom-default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user