Introduce reserved default profile support
AbstractEnvironment and subclasses now register a reserved default
profile named literally 'default' such that with no action on the part
of the user, beans defined against the 'default' profile will be
registered - if no other profiles are explicitly activated.
For example, given the following three files a.xml, b.xml and c.xml:
a.xml
-----
<beans> <!-- no 'profile' attribute -->
<bean id="a" class="com.acme.A"/>
</beans>
b.xml
-----
<beans profile="default">
<bean id="b" class="com.acme.B"/>
</beans>
c.xml
-----
<beans profile="custom">
<bean id="c" class="com.acme.C"/>
</beans>
bootstrapping all of the files in a Spring ApplicationContext as
follows will result in beans 'a' and 'b', but not 'c' being registered:
ApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("a.xml");
ctx.load("b.xml");
ctx.load("c.xml");
ctx.refresh();
ctx.containsBean("a"); // true
ctx.containsBean("b"); // true
ctx.containsBean("c"); // false
whereas activating the 'custom' profile will result in beans 'a' and
'c', but not 'b' being registered:
ApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("a.xml");
ctx.load("b.xml");
ctx.load("c.xml");
ctx.getEnvironment().setActiveProfiles("custom");
ctx.refresh();
ctx.containsBean("a"); // true
ctx.containsBean("b"); // false
ctx.containsBean("c"); // true
that is, once the 'custom' profile is activated, beans defined against
the the reserved default profile are no longer registered. Beans not
defined against any profile ('a') are always registered regardless of
which profiles are active, and of course beans registered
against specific active profiles ('c') are registered.
The reserved default profile is, in practice, just another 'default
profile', as might be added through calling env.setDefaultProfiles() or
via the 'spring.profiles.default' property. The only difference is that
the reserved default is added automatically by AbstractEnvironment
implementations. As such, any call to setDefaultProfiles() or value set
for the 'spring.profiles.default' will override the reserved default
profile. If a user wishes to add their own default profile while
keeping the reserved default profile as well, it will need to be
explicitly redeclared, e.g.:
env.addDefaultProfiles("my-default-profile", "default")
The reserved default profile(s) are determined by the value returned
from AbstractEnvironment#getReservedDefaultProfiles(). This protected
method may be overridden by subclasses in order to customize the
set of reserved default profiles.
Issue: SPR-8203
This commit is contained in:
@@ -16,23 +16,28 @@
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import static java.lang.String.*;
|
||||
import static java.lang.String.format;
|
||||
import static org.springframework.util.StringUtils.commaDelimitedListToSet;
|
||||
import static org.springframework.util.StringUtils.trimAllWhitespace;
|
||||
|
||||
import java.security.AccessControlException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link Environment} implementations.
|
||||
* Abstract base class for {@link Environment} implementations. Supports the notion of
|
||||
* reserved default profile names and enables specifying active and default profiles
|
||||
* through the {@link #ACTIVE_PROFILES_PROPERTY_NAME} and
|
||||
* {@link #DEFAULT_PROFILES_PROPERTY_NAME} properties.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
@@ -52,10 +57,22 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
*/
|
||||
public static final String DEFAULT_PROFILES_PROPERTY_NAME = "spring.profiles.default";
|
||||
|
||||
/**
|
||||
* Name of reserved default profile name: {@value}. If no default profile names are
|
||||
* explicitly and no active profile names are explictly set, this profile will
|
||||
* automatically be activated by default.
|
||||
* @see #getReservedDefaultProfiles
|
||||
* @see ConfigurableEnvironment#setDefaultProfiles
|
||||
* @see ConfigurableEnvironment#setActiveProfiles
|
||||
* @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME
|
||||
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||
*/
|
||||
protected static final String RESERVED_DEFAULT_PROFILE_NAME = "default";
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private Set<String> activeProfiles = new LinkedHashSet<String>();
|
||||
private Set<String> defaultProfiles = new LinkedHashSet<String>();
|
||||
private Set<String> defaultProfiles = new LinkedHashSet<String>(this.getReservedDefaultProfiles());
|
||||
|
||||
private MutablePropertySources propertySources = new MutablePropertySources();
|
||||
private ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
|
||||
@@ -69,6 +86,14 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
return StringUtils.toStringArray(doGetActiveProfiles());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set of active profiles as explicitly set through
|
||||
* {@link #setActiveProfiles} or if the current set of active profiles
|
||||
* is empty, check for the presence of the {@value #ACTIVE_PROFILES_PROPERTY_NAME}
|
||||
* property and assign its value to the set of active profiles.
|
||||
* @see #getActiveProfiles()
|
||||
* @see #ACTIVE_PROFILES_PROPERTY_NAME
|
||||
*/
|
||||
protected Set<String> doGetActiveProfiles() {
|
||||
if (this.activeProfiles.isEmpty()) {
|
||||
String profiles = this.propertyResolver.getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
|
||||
@@ -88,16 +113,35 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
return StringUtils.toStringArray(doGetDefaultProfiles());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set of default profiles explicitly set via
|
||||
* {@link #setDefaultProfiles(String...)} or if the current set of default profiles
|
||||
* consists only of {@linkplain #getReservedDefaultProfiles() reserved default
|
||||
* profiles}, then check for the presence of the
|
||||
* {@value #DEFAULT_PROFILES_PROPERTY_NAME} property and assign its value (if any)
|
||||
* to the set of default profiles.
|
||||
* @see #AbstractEnvironment()
|
||||
* @see #getDefaultProfiles()
|
||||
* @see #DEFAULT_PROFILES_PROPERTY_NAME
|
||||
* @see #getReservedDefaultProfiles()
|
||||
*/
|
||||
protected Set<String> doGetDefaultProfiles() {
|
||||
if (this.defaultProfiles.isEmpty()) {
|
||||
String profiles = this.propertyResolver.getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
|
||||
if (StringUtils.hasText(profiles)) {
|
||||
this.defaultProfiles = commaDelimitedListToSet(trimAllWhitespace(profiles));
|
||||
if (this.defaultProfiles.equals(this.getReservedDefaultProfiles())) {
|
||||
String defaultProfiles = this.propertyResolver.getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
|
||||
if (defaultProfiles != null) {
|
||||
this.defaultProfiles = commaDelimitedListToSet(trimAllWhitespace(defaultProfiles));
|
||||
}
|
||||
}
|
||||
return this.defaultProfiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>Calling this method removes overrides any reserved default profiles
|
||||
* that may have been added during construction of the environment.
|
||||
* @see #AbstractEnvironment()
|
||||
* @see #getReservedDefaultProfiles()
|
||||
*/
|
||||
public void setDefaultProfiles(String... profiles) {
|
||||
this.defaultProfiles.clear();
|
||||
this.defaultProfiles.addAll(Arrays.asList(profiles));
|
||||
|
||||
@@ -66,14 +66,15 @@ package org.springframework.core.env;
|
||||
public interface Environment extends PropertyResolver {
|
||||
|
||||
/**
|
||||
* Return the set of profiles explicitly made active for this environment. Profiles are used for
|
||||
* creating logical groupings of bean definitions to be registered conditionally, often based on
|
||||
* deployment environment. Profiles can be activated by setting {@linkplain
|
||||
* AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME "spring.profiles.active"} as a system property
|
||||
* or by calling {@link ConfigurableEnvironment#setActiveProfiles(String...)}.
|
||||
* Return the set of profiles explicitly made active for this environment. Profiles
|
||||
* are used for creating logical groupings of bean definitions to be registered
|
||||
* conditionally, for example based on deployment environment. Profiles can be
|
||||
* activated by setting {@linkplain AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||
* "spring.profiles.active"} as a system property or by calling
|
||||
* {@link ConfigurableEnvironment#setActiveProfiles(String...)}.
|
||||
*
|
||||
* <p>If no profiles have explicitly been specified as active, then any 'default' profiles will implicitly
|
||||
* be considered active.
|
||||
* <p>If no profiles have explicitly been specified as active, then any {@linkplain
|
||||
* #getDefaultProfiles() default profiles} will automatically be activated.
|
||||
*
|
||||
* @see #getDefaultProfiles
|
||||
* @see ConfigurableEnvironment#setActiveProfiles
|
||||
@@ -82,18 +83,22 @@ public interface Environment extends PropertyResolver {
|
||||
String[] getActiveProfiles();
|
||||
|
||||
/**
|
||||
* Return the set of profiles to be active by default when no active profiles have been set explicitly.
|
||||
* Return the set of profiles to be active by default when no active profiles have
|
||||
* been set explicitly.
|
||||
*
|
||||
* @see #getActiveProfiles
|
||||
* @see ConfigurableEnvironment#setDefaultProfiles
|
||||
* @see AbstractEnvironment#DEFAULT_PROFILES_PROPERTY_NAME
|
||||
*/
|
||||
String[] getDefaultProfiles();
|
||||
|
||||
/**
|
||||
* @return whether one or more of the given profiles is active, or in the case of no explicit active
|
||||
* profiles, whether one or more of the given profiles is included in the set of default profiles
|
||||
* @return whether one or more of the given profiles is active, or in the case of no
|
||||
* explicit active profiles, whether one or more of the given profiles is included in
|
||||
* the set of default profiles
|
||||
* @throws IllegalArgumentException unless at least one profile has been specified
|
||||
* @throws IllegalArgumentException if any profile is the empty string or consists only of whitespace
|
||||
* @throws IllegalArgumentException if any profile is the empty string or consists
|
||||
* only of whitespace
|
||||
* @see #getActiveProfiles
|
||||
* @see #getDefaultProfiles
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user