Environment implementations. Supports the notion of
reserved default profile names and enables specifying active and default profiles
through the ACTIVE_PROFILES_PROPERTY_NAME and
DEFAULT_PROFILES_PROPERTY_NAME properties.
Concrete subclasses differ primarily on which PropertySource objects they
add by default. AbstractEnvironment adds none. Subclasses should contribute
property sources through the protected customizePropertySources(MutablePropertySources)
hook, while clients should customize using ConfigurableEnvironment#getPropertySources()
and working against the MutablePropertySources API.
See ConfigurableEnvironment javadoc for usage examples.
ConfigurableEnvironment, StandardEnvironment
| Modifier and Type | Field and Description |
|---|---|
| public static final String | ACTIVE_PROFILES_PROPERTY_NAME
Name of the property to set to specify active profiles: spring.profiles.active. |
| private final Set | |
| public static final String | DEFAULT_PROFILES_PROPERTY_NAME
Name of the property to set to specify profiles that are active by default: spring.profiles.default. |
| private final Set | |
| public static final String | IGNORE_GETENV_PROPERTY_NAME
System property that instructs Spring to ignore system environment variables,
i.e. to never attempt to retrieve such a variable via |
| protected final Log | |
| private final ConfigurablePropertyResolver | |
| private final MutablePropertySources | |
| public static final String | RESERVED_DEFAULT_PROFILE_NAME
Name of the reserved default profile name: default. |
| Access | Constructor and Description |
|---|---|
| public | AbstractEnvironment()
Create a new |
| protected | AbstractEnvironment(MutablePropertySources
property sources to use propertySources)Create a new |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | acceptsProfiles(String... profiles)
Implements org.
Deprecated
Determine 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.
|
| public boolean | acceptsProfiles(Profiles profiles)
Implements org. Profiles predicate matches the
active profiles — or in the case
of no explicit active profiles, whether the given Profiles predicate
matches the default profiles.
|
| public void | addActiveProfile(String profile)
Implements org. |
| public boolean | containsProperty(String key)
Implements org. null.
|
| protected ConfigurablePropertyResolver | createPropertyResolver(MutablePropertySources propertySources)
Factory method used to create the |
| protected void | customizePropertySources(MutablePropertySources propertySources)
Customize the set of |
| protected Set | doGetActiveProfiles()
Return the set of active profiles as explicitly set through
|
| protected String | |
| protected Set | doGetDefaultProfiles()
Return the set of default profiles explicitly set via
|
| protected String | |
| public String[] | getActiveProfiles()
Implements org. |
| public ConfigurableConversionService | getConversionService()
Implements org. ConfigurableConversionService used when performing type
conversions on properties.
|
| public String[] | getDefaultProfiles()
Implements org. |
| public String | getProperty(String
the property name to resolve key)Implements org. null if the key cannot be resolved.
|
| public String | getProperty(String
the property name to resolve key, String the default value to return if no value is found defaultValue)Implements org. defaultValue if the key cannot be resolved.
|
| public <T> T | getProperty(String
the property name to resolve key, Class<T> the expected type of the property value targetType)Implements org. null if the key cannot be resolved.
|
| public <T> T | getProperty(String
the property name to resolve key, Class<T> the expected type of the property value targetType, T the default value to return if no value is found defaultValue)Implements org. defaultValue if the key cannot be resolved.
|
| protected final ConfigurablePropertyResolver | |
| public MutablePropertySources | getPropertySources()
Implements org. PropertySources for this Environment in mutable form,
allowing for manipulation of the set of PropertySource objects that should
be searched when resolving properties against this Environment object.
|
| public String | getRequiredProperty(String key)
Implements org. null).
|
| public <T> T | getRequiredProperty(String key, Class<T> targetType)
Implements org. null).
|
| protected Set | |
| public Map | getSystemEnvironment()
Implements org. System#getenv().
|
| public Map | getSystemProperties()
Implements org. System#getProperties().
|
| protected boolean | isProfileActive(String profile)
Return whether the given profile is active, or if active profiles are empty whether the profile should be active by default. |
| public void | merge(ConfigurableEnvironment
the environment to merge with parent)Implements org. |
| public String | resolvePlaceholders(String
the String to resolve text)Implements org. getProperty.
|
| public String | resolveRequiredPlaceholders(String text)
Implements org. getProperty.
|
| public void | setActiveProfiles(String... profiles)
Implements org. Environment.
|
| public void | setConversionService(ConfigurableConversionService conversionService)
Implements org. ConfigurableConversionService to be used when performing type
conversions on properties.
|
| public void | setDefaultProfiles(String... profiles)
Implements org. setActiveProfiles.
|
| public void | setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders)
Implements org. |
| public void | setPlaceholderPrefix(String placeholderPrefix)
Implements org. |
| public void | setPlaceholderSuffix(String placeholderSuffix)
Implements org. |
| public void | setRequiredProperties(String... requiredProperties)
Implements org. validateRequiredProperties().
|
| public void | setValueSeparator(String valueSeparator)
Implements org. null if no such
special character should be processed as a value separator.
|
| protected boolean | suppressGetenvAccess()
Determine whether to suppress |
| public String | |
| protected void | validateProfile(String profile)
Validate the given profile, called internally prior to adding to the set of active or default profiles. |
| public void | validateRequiredProperties()
Implements org. setRequiredProperties is present and resolves to a
non-null value.
|
| ACTIVE_PROFILES_PROPERTY_NAME | back to summary |
|---|---|
| public static final String ACTIVE_PROFILES_PROPERTY_NAME Name of the property to set to specify active profiles: spring.profiles.active. The value may be comma delimited. Note that certain shell environments such as Bash disallow the use of the period
character in variable names. Assuming that Spring's | |
| activeProfiles | back to summary |
|---|---|
| private final Set<String> activeProfiles | |
| DEFAULT_PROFILES_PROPERTY_NAME | back to summary |
|---|---|
| public static final String DEFAULT_PROFILES_PROPERTY_NAME Name of the property to set to specify profiles that are active by default: spring.profiles.default. The value may be comma delimited. Note that certain shell environments such as Bash disallow the use of the period
character in variable names. Assuming that Spring's | |
| defaultProfiles | back to summary |
|---|---|
| private final Set<String> defaultProfiles | |
| IGNORE_GETENV_PROPERTY_NAME | back to summary |
|---|---|
| public static final String IGNORE_GETENV_PROPERTY_NAME System property that instructs Spring to ignore system environment variables,
i.e. to never attempt to retrieve such a variable via The default is "false", falling back to system environment variable checks if a
Spring environment property (e.g. a placeholder in a configuration String) isn't
resolvable otherwise. Consider switching this flag to "true" if you experience
log warnings from
| |
| logger | back to summary |
|---|---|
| protected final Log logger | |
| propertyResolver | back to summary |
|---|---|
| private final ConfigurablePropertyResolver propertyResolver | |
| propertySources | back to summary |
|---|---|
| private final MutablePropertySources propertySources | |
| RESERVED_DEFAULT_PROFILE_NAME | back to summary |
|---|---|
| public static final String RESERVED_DEFAULT_PROFILE_NAME Name of the reserved default profile name: default. If no default profile names are explicitly set and no active profile names are explicitly set, this profile will automatically be activated by default. | |
| AbstractEnvironment | back to summary |
|---|---|
| public AbstractEnvironment() Create a new | |
| AbstractEnvironment | back to summary |
|---|---|
| protected AbstractEnvironment(MutablePropertySources propertySources) Create a new
| |
| acceptsProfiles | back to summary |
|---|---|
| public boolean acceptsProfiles(String... profiles) Implements org. Doc from org. Deprecated
Determine 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. If a profile begins with '!' the logic is inverted, meaning this method
will return
| |
| acceptsProfiles | back to summary |
|---|---|
| public boolean acceptsProfiles(Profiles profiles) Implements org. Doc from org. Determine whether the given If you wish provide profile expressions directly as strings, use
| |
| addActiveProfile | back to summary |
|---|---|
| public void addActiveProfile(String profile) Implements org. Doc from org. Add a profile to the current set of active profiles.
| |
| containsProperty | back to summary |
|---|---|
| public boolean containsProperty(String key) Implements org. Doc from org. Return whether the given property key is available for resolution,
i.e. if the value for the given key is not
| |
| createPropertyResolver | back to summary |
|---|---|
| protected ConfigurablePropertyResolver createPropertyResolver(MutablePropertySources propertySources) Factory method used to create the
| |
| customizePropertySources | back to summary |
|---|---|
| protected void customizePropertySources(MutablePropertySources propertySources) Customize the set of Subclasses that override this method are encouraged to add property
sources using
public class Level1Environment extends AbstractEnvironment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources); // no-op from base class
propertySources.addLast(new PropertySourceA(...));
propertySources.addLast(new PropertySourceB(...));
}
}
public class Level2Environment extends Level1Environment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
super.customizePropertySources(propertySources); // add all from superclass
propertySources.addLast(new PropertySourceC(...));
propertySources.addLast(new PropertySourceD(...));
}
}
In this arrangement, properties will be resolved against sources A, B, C, D in that
order. That is to say that property source "A" has precedence over property source
"D". If the
public class Level2Environment extends Level1Environment {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new PropertySourceC(...));
propertySources.addLast(new PropertySourceD(...));
super.customizePropertySources(propertySources); // add all from superclass
}
}
The search order is now C, D, A, B as desired. Beyond these recommendations, subclasses may use any of the The base implementation registers no property sources. Note that clients of any ConfigurableEnvironment env = new StandardEnvironment(); env.getPropertySources().addLast(new PropertySourceX(...)); A warning about instance variable accessInstance variables declared in subclasses and having default initial values should
not be accessed from within this method. Due to Java object creation
lifecycle constraints, any initial value will not yet be assigned when this
callback is invoked by the | |
| doGetActiveProfiles | back to summary |
|---|---|
| protected Set Return the set of active profiles as explicitly set through
| |
| doGetActiveProfilesProperty | back to summary |
|---|---|
| protected String doGetActiveProfilesProperty() Return the property value for the active profiles.
| |
| doGetDefaultProfiles | back to summary |
|---|---|
| protected Set Return the set of default profiles explicitly set via
| |
| doGetDefaultProfilesProperty | back to summary |
|---|---|
| protected String doGetDefaultProfilesProperty() Return the property value for the default profiles.
| |
| getActiveProfiles | back to summary |
|---|---|
| public String[] getActiveProfiles() Implements org. Doc from org. 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 "spring.profiles.active" as a system property or by calling
If no profiles have explicitly been specified as active, then any default profiles will automatically be activated.
| |
| getConversionService | back to summary |
|---|---|
| public ConfigurableConversionService getConversionService() Implements org. Doc from org. Return the The configurable nature of the returned conversion service allows for
the convenient addition and removal of individual ConfigurableConversionService cs = env.getConversionService(); cs.addConverter(new FooConverter());
| |
| getDefaultProfiles | back to summary |
|---|---|
| public String[] getDefaultProfiles() Implements org. Doc from org. Return the set of profiles to be active by default when no active profiles have been set explicitly.
| |
| getProperty | back to summary |
|---|---|
| public String getProperty(String key) Implements org. Doc from org. Return the property value associated with the given key,
or | |
| getProperty | back to summary |
|---|---|
| public String getProperty(String key, String defaultValue) Implements org. Doc from org. Return the property value associated with the given key, or
| |
| getProperty | back to summary |
|---|---|
| public <T> T getProperty(String key, Class<T> targetType) Implements org. Doc from org. Return the property value associated with the given key,
or | |
| getProperty | back to summary |
|---|---|
| public <T> T getProperty(String key, Class<T> targetType, T defaultValue) Implements org. Doc from org. Return the property value associated with the given key,
or | |
| getPropertyResolver | back to summary |
|---|---|
| protected final ConfigurablePropertyResolver getPropertyResolver() Return the
| |
| getPropertySources | back to summary |
|---|---|
| public MutablePropertySources getPropertySources() Implements org. Doc from org. Return the
| |
| getRequiredProperty | back to summary |
|---|---|
| public String getRequiredProperty(String key) throws IllegalStateException Implements org. Doc from org. Return the property value associated with the given key (never
| |
| getRequiredProperty | back to summary |
|---|---|
| public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException Implements org. Doc from org. Return the property value associated with the given key, converted to the given
targetType (never
| |
| getReservedDefaultProfiles | back to summary |
|---|---|
| protected Set Return the set of reserved default profile names. This implementation returns default. Subclasses may override in order to customize the set of reserved names. | |
| getSystemEnvironment | back to summary |
|---|---|
| public Map Implements org. Doc from org. Return the value of Note that most
| |
| getSystemProperties | back to summary |
|---|---|
| public Map Implements org. Doc from org. Return the value of Note that most
| |
| isProfileActive | back to summary |
|---|---|
| protected boolean isProfileActive(String profile) Return whether the given profile is active, or if active profiles are empty whether the profile should be active by default.
| |
| merge | back to summary |
|---|---|
| public void merge(ConfigurableEnvironment parent) Implements org. Doc from org. Append the given parent environment's active profiles, default profiles and property sources to this (child) environment's respective collections of each. For any identically-named Active and default profile names are also filtered for duplicates, to avoid confusion and redundant storage. The parent environment remains unmodified in any case. Note that any changes to
the parent environment occurring after the call to
| |
| resolvePlaceholders | back to summary |
|---|---|
| public String resolvePlaceholders(String text) Implements org. Doc from org. Resolve ${...} placeholders in the given text, replacing them with corresponding
property values as resolved by | |
| resolveRequiredPlaceholders | back to summary |
|---|---|
| public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException Implements org. Doc from org. Resolve ${...} placeholders in the given text, replacing them with corresponding
property values as resolved by
| |
| setActiveProfiles | back to summary |
|---|---|
| public void setActiveProfiles(String... profiles) Implements org. Doc from org. Specify the set of profiles active for this Any existing active profiles will be replaced with the given arguments; call
with zero arguments to clear the current set of active profiles. Use
| |
| setConversionService | back to summary |
|---|---|
| public void setConversionService(ConfigurableConversionService conversionService) Implements org. Doc from org. Set the Note as an alternative to fully replacing the
| |
| setDefaultProfiles | back to summary |
|---|---|
| public void setDefaultProfiles(String... profiles) Implements org. Specify the set of profiles to be made active by default if no other profiles
are explicitly made active through Calling this method removes overrides any reserved default profiles that may have been added during construction of the environment.
| |
| setIgnoreUnresolvableNestedPlaceholders | back to summary |
|---|---|
| public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) Implements org. Doc from org. Set whether to throw an exception when encountering an unresolvable placeholder
nested within the value of a given property. A Implementations of
| |
| setPlaceholderPrefix | back to summary |
|---|---|
| public void setPlaceholderPrefix(String placeholderPrefix) Implements org. Doc from org. Set the prefix that placeholders replaced by this resolver must begin with.
| |
| setPlaceholderSuffix | back to summary |
|---|---|
| public void setPlaceholderSuffix(String placeholderSuffix) Implements org. Doc from org. Set the suffix that placeholders replaced by this resolver must end with.
| |
| setRequiredProperties | back to summary |
|---|---|
| public void setRequiredProperties(String... requiredProperties) Implements org. Doc from org. Specify which properties must be present, to be verified by
| |
| setValueSeparator | back to summary |
|---|---|
| public void setValueSeparator(String valueSeparator) Implements org. Doc from org. Specify the separating character between the placeholders replaced by this
resolver and their associated default value, or
| |
| suppressGetenvAccess | back to summary |
|---|---|
| protected boolean suppressGetenvAccess() Determine whether to suppress If this method returns The default implementation checks for the "spring.getenv.ignore" system property,
returning | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| validateProfile | back to summary |
|---|---|
| protected void validateProfile(String profile) Validate the given profile, called internally prior to adding to the set of active or default profiles. Subclasses may override to impose further restrictions on profile syntax.
| |
| validateRequiredProperties | back to summary |
|---|---|
| public void validateRequiredProperties() throws MissingRequiredPropertiesException Implements org. Doc from org. Validate that each of the properties specified by
| |