|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.springframework.core.env.AbstractEnvironment
public abstract class AbstractEnvironment
Abstract base class for 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.
StandardEnvironment| Field Summary | |
|---|---|
static String |
ACTIVE_PROFILES_PROPERTY_NAME
Name of property to set to specify active profiles: "spring.profiles.active". |
static String |
DEFAULT_PROFILES_PROPERTY_NAME
Name of property to set to specify default profiles: "spring.profiles.default". |
protected Log |
logger
|
protected static String |
RESERVED_DEFAULT_PROFILE_NAME
Name of reserved default profile name: "default". |
| Constructor Summary | |
|---|---|
AbstractEnvironment()
|
|
| Method Summary | ||
|---|---|---|
boolean |
acceptsProfiles(String... profiles)
|
|
boolean |
containsProperty(String key)
Return whether the given property key is available for resolution, i.e., the value for the given key is not null. |
|
protected void |
customizePropertySources(MutablePropertySources propertySources)
Customize the set of PropertySource objects to be searched by this
Environment during calls to getProperty(String) and related
methods. |
|
protected Set<String> |
doGetActiveProfiles()
Return the set of active profiles as explicitly set through setActiveProfiles(java.lang.String...) or if the current set of active profiles
is empty, check for the presence of the "spring.profiles.active"
property and assign its value to the set of active profiles. |
|
protected Set<String> |
doGetDefaultProfiles()
Return the set of default profiles explicitly set via setDefaultProfiles(String...) or if the current set of default profiles
consists only of reserved default
profiles, then check for the presence of the
"spring.profiles.default" property and assign its value (if any)
to the set of default profiles. |
|
String[] |
getActiveProfiles()
Return the set of profiles explicitly made active for this environment. |
|
ConfigurableConversionService |
getConversionService()
|
|
String[] |
getDefaultProfiles()
Return the set of profiles to be active by default when no active profiles have been set explicitly. |
|
String |
getProperty(String key)
Return the property value associated with the given key, or null
if the key cannot be resolved. |
|
|
getProperty(String key,
Class<T> targetType)
Return the property value associated with the given key, or null
if the key cannot be resolved. |
|
|
getProperty(String key,
Class<T> targetType,
T defaultValue)
Return the property value associated with the given key, or defaultValue if the key cannot be resolved. |
|
String |
getProperty(String key,
String defaultValue)
Return the property value associated with the given key, or defaultValue if the key cannot be resolved. |
|
|
getPropertyAsClass(String key,
Class<T> targetType)
Convert the property value associated with the given key to a Class
of type T or null if the key cannot be resolved. |
|
MutablePropertySources |
getPropertySources()
Return the 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. |
|
String |
getRequiredProperty(String key)
Return the property value associated with the given key, converted to the given targetType (never null). |
|
|
getRequiredProperty(String key,
Class<T> targetType)
Return the property value associated with the given key, converted to the given targetType (never null). |
|
protected Set<String> |
getReservedDefaultProfiles()
Return the set of reserved default profile names. |
|
Map<String,Object> |
getSystemEnvironment()
Return the value of System.getenv() if allowed by the current
SecurityManager, otherwise return a map implementation that will attempt
to access individual keys using calls to System.getenv(String). |
|
Map<String,Object> |
getSystemProperties()
Return the value of System.getProperties() if allowed by the current
SecurityManager, otherwise return a map implementation that will attempt
to access individual keys using calls to System.getProperty(String). |
|
String |
resolvePlaceholders(String text)
Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by PropertyResolver.getProperty(java.lang.String). |
|
String |
resolveRequiredPlaceholders(String text)
Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by PropertyResolver.getProperty(java.lang.String). |
|
void |
setActiveProfiles(String... profiles)
Specify the set of profiles active for this Environment. |
|
void |
setConversionService(ConfigurableConversionService conversionService)
Set the ConfigurableConversionService to be used when performing type
conversions on properties. |
|
void |
setDefaultProfiles(String... profiles)
Specify the set of profiles to be made active by default if no other profiles are explicitly made active through ConfigurableEnvironment.setActiveProfiles(java.lang.String...). |
|
void |
setPlaceholderPrefix(String placeholderPrefix)
Set the prefix that placeholders replaced by this resolver must begin with. |
|
void |
setPlaceholderSuffix(String placeholderSuffix)
Set the suffix that placeholders replaced by this resolver must end with. |
|
void |
setRequiredProperties(String... requiredProperties)
Specify which properties must be present, to be verified by ConfigurablePropertyResolver.validateRequiredProperties(). |
|
void |
setValueSeparator(String valueSeparator)
Specify the separating character between the placeholders replaced by this resolver and their associated default value, or null if no such
special character should be processed as a value separator. |
|
String |
toString()
|
|
void |
validateRequiredProperties()
Validate that each of the properties specified by ConfigurablePropertyResolver.setRequiredProperties(java.lang.String...) is present and resolves to a
non-null value. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final String ACTIVE_PROFILES_PROPERTY_NAME
ConfigurableEnvironment.setActiveProfiles(java.lang.String...),
Constant Field Valuespublic static final String DEFAULT_PROFILES_PROPERTY_NAME
ConfigurableEnvironment.setDefaultProfiles(java.lang.String...),
Constant Field Valuesprotected static final String RESERVED_DEFAULT_PROFILE_NAME
getReservedDefaultProfiles(),
ConfigurableEnvironment.setDefaultProfiles(java.lang.String...),
ConfigurableEnvironment.setActiveProfiles(java.lang.String...),
DEFAULT_PROFILES_PROPERTY_NAME,
ACTIVE_PROFILES_PROPERTY_NAME,
Constant Field Valuesprotected final Log logger
| Constructor Detail |
|---|
public AbstractEnvironment()
| Method Detail |
|---|
protected void customizePropertySources(MutablePropertySources propertySources)
PropertySource objects to be searched by this
Environment during calls to getProperty(String) and related
methods.
Subclasses that override this method are encouraged to add property
sources using MutablePropertySources.addLast(PropertySource) such that
further subclasses may call super.customizePropertySources() with
predictable results. For example:
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 Level2Environment subclass wished to give property sources C
and D higher precedence than A and B, it could simply call
super.customizePropertySources after, rather than before adding its own:
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 add*,
remove, or replace methods exposed by MutablePropertySources
in order to create the exact arrangement of property sources desired.
The base implementation in customizePropertySources(org.springframework.core.env.MutablePropertySources)
registers no property sources.
Note that clients of any ConfigurableEnvironment may further customize
property sources via the getPropertySources() accessor, typically within
an ApplicationContextInitializer. For example:
ConfigurableEnvironment env = new StandardEnvironment(); env.getPropertySources().addLast(new PropertySourceX(...));
MutablePropertySources,
PropertySourcesPropertyResolver,
ApplicationContextInitializerprotected Set<String> getReservedDefaultProfiles()
RESERVED_DEFAULT_PROFILE_NAME,
doGetDefaultProfiles()public String[] getActiveProfiles()
EnvironmentConfigurableEnvironment.setActiveProfiles(String...).
If no profiles have explicitly been specified as active, then any default profiles will automatically be activated.
getActiveProfiles in interface EnvironmentEnvironment.getDefaultProfiles(),
ConfigurableEnvironment.setActiveProfiles(java.lang.String...),
ACTIVE_PROFILES_PROPERTY_NAMEprotected Set<String> doGetActiveProfiles()
setActiveProfiles(java.lang.String...) or if the current set of active profiles
is empty, check for the presence of the "spring.profiles.active"
property and assign its value to the set of active profiles.
getActiveProfiles(),
ACTIVE_PROFILES_PROPERTY_NAMEpublic void setActiveProfiles(String... profiles)
ConfigurableEnvironmentEnvironment. Profiles are
evaluated during container bootstrap to determine whether bean definitions
should be registered with the container.
setActiveProfiles in interface ConfigurableEnvironmentConfigurableEnvironment.setDefaultProfiles(java.lang.String...),
Profile,
ACTIVE_PROFILES_PROPERTY_NAMEpublic String[] getDefaultProfiles()
Environment
getDefaultProfiles in interface EnvironmentEnvironment.getActiveProfiles(),
ConfigurableEnvironment.setDefaultProfiles(java.lang.String...),
DEFAULT_PROFILES_PROPERTY_NAMEprotected Set<String> doGetDefaultProfiles()
setDefaultProfiles(String...) or if the current set of default profiles
consists only of reserved default
profiles, then check for the presence of the
"spring.profiles.default" property and assign its value (if any)
to the set of default profiles.
AbstractEnvironment(),
getDefaultProfiles(),
DEFAULT_PROFILES_PROPERTY_NAME,
getReservedDefaultProfiles()public void setDefaultProfiles(String... profiles)
ConfigurableEnvironment.setActiveProfiles(java.lang.String...).
Calling this method removes overrides any reserved default profiles that may have been added during construction of the environment.
setDefaultProfiles in interface ConfigurableEnvironmentAbstractEnvironment(),
getReservedDefaultProfiles()public boolean acceptsProfiles(String... profiles)
acceptsProfiles in interface EnvironmentEnvironment.getActiveProfiles(),
Environment.getDefaultProfiles()public MutablePropertySources getPropertySources()
ConfigurableEnvironmentPropertySources 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.
The various MutablePropertySources methods such as
addFirst,
addLast,
addBefore and
addAfter allow for fine-grained control
over property source ordering. This is useful, for example, in ensuring that
certain user-defined property sources have search precedence over default property
sources such as the set of system properties or the set of system environment
variables.
getPropertySources in interface ConfigurableEnvironmentcustomizePropertySources(org.springframework.core.env.MutablePropertySources)public Map<String,Object> getSystemEnvironment()
ConfigurableEnvironmentSystem.getenv() if allowed by the current
SecurityManager, otherwise return a map implementation that will attempt
to access individual keys using calls to System.getenv(String).
Note that most Environment implementations will include this system
environment map as a default PropertySource to be searched. Therefore, it
is recommended that this method not be used directly unless bypassing other
property sources is expressly intended.
Calls to Map.get(Object) on the Map returned will never throw
IllegalAccessException; in cases where the SecurityManager forbids access
to a property, null will be returned and an INFO-level log message will be
issued noting the exception.
getSystemEnvironment in interface ConfigurableEnvironmentpublic Map<String,Object> getSystemProperties()
ConfigurableEnvironmentSystem.getProperties() if allowed by the current
SecurityManager, otherwise return a map implementation that will attempt
to access individual keys using calls to System.getProperty(String).
Note that most Environment implementations will include this system
properties map as a default PropertySource to be searched. Therefore, it is
recommended that this method not be used directly unless bypassing other property
sources is expressly intended.
Calls to Map.get(Object) on the Map returned will never throw
IllegalAccessException; in cases where the SecurityManager forbids access
to a property, null will be returned and an INFO-level log message will be
issued noting the exception.
getSystemProperties in interface ConfigurableEnvironmentpublic boolean containsProperty(String key)
PropertyResolvernull.
containsProperty in interface PropertyResolverpublic String getProperty(String key)
PropertyResolvernull
if the key cannot be resolved.
getProperty in interface PropertyResolverkey - the property name to resolvePropertyResolver.getProperty(String, String),
PropertyResolver.getProperty(String, Class),
PropertyResolver.getRequiredProperty(String)
public String getProperty(String key,
String defaultValue)
PropertyResolverdefaultValue if the key cannot be resolved.
getProperty in interface PropertyResolverkey - the property name to resolvedefaultValue - the default value to return if no value is foundPropertyResolver.getRequiredProperty(String),
PropertyResolver.getProperty(String, Class)
public <T> T getProperty(String key,
Class<T> targetType)
PropertyResolvernull
if the key cannot be resolved.
getProperty in interface PropertyResolverkey - the property name to resolvePropertyResolver.getRequiredProperty(String, Class)
public <T> T getProperty(String key,
Class<T> targetType,
T defaultValue)
PropertyResolverdefaultValue if the key cannot be resolved.
getProperty in interface PropertyResolverdefaultValue - the default value to return if no value is foundPropertyResolver.getRequiredProperty(String, Class)
public <T> Class<T> getPropertyAsClass(String key,
Class<T> targetType)
PropertyResolverClass
of type T or null if the key cannot be resolved.
getPropertyAsClass in interface PropertyResolverPropertyResolver.getProperty(String, Class)
public String getRequiredProperty(String key)
throws IllegalStateException
PropertyResolvernull).
getRequiredProperty in interface PropertyResolverIllegalStateException - if the key cannot be resolvedPropertyResolver.getRequiredProperty(String, Class)
public <T> T getRequiredProperty(String key,
Class<T> targetType)
throws IllegalStateException
PropertyResolvernull).
getRequiredProperty in interface PropertyResolverIllegalStateException - if the given key cannot be resolvedpublic void setRequiredProperties(String... requiredProperties)
ConfigurablePropertyResolverConfigurablePropertyResolver.validateRequiredProperties().
setRequiredProperties in interface ConfigurablePropertyResolver
public void validateRequiredProperties()
throws MissingRequiredPropertiesException
ConfigurablePropertyResolverConfigurablePropertyResolver.setRequiredProperties(java.lang.String...) is present and resolves to a
non-null value.
validateRequiredProperties in interface ConfigurablePropertyResolverMissingRequiredPropertiesException - if any of the required
properties are not resolvable.public String resolvePlaceholders(String text)
PropertyResolverPropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with
no default value are ignored and passed through unchanged.
resolvePlaceholders in interface PropertyResolvertext - the String to resolve
null)PropertyResolver.resolveRequiredPlaceholders(java.lang.String),
SystemPropertyUtils.resolvePlaceholders(String)
public String resolveRequiredPlaceholders(String text)
throws IllegalArgumentException
PropertyResolverPropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with
no default value will cause an IllegalArgumentException to be thrown.
resolveRequiredPlaceholders in interface PropertyResolvernull)
IllegalArgumentException - if given text is nullSystemPropertyUtils.resolvePlaceholders(String, boolean)public void setConversionService(ConfigurableConversionService conversionService)
ConfigurablePropertyResolverConfigurableConversionService to be used when performing type
conversions on properties.
Note: as an alternative to fully replacing the ConversionService, consider adding or removing individual Converter
intstances by drilling into ConfigurablePropertyResolver.getConversionService() and calling methods
such as #addConverter.
setConversionService in interface ConfigurablePropertyResolverPropertyResolver.getProperty(String, Class),
ConfigurablePropertyResolver.getConversionService(),
ConverterRegistry.addConverter(org.springframework.core.convert.converter.Converter, ?>)public ConfigurableConversionService getConversionService()
getConversionService in interface ConfigurablePropertyResolverConfigurableConversionService used when performing type
conversions on properties.
The configurable nature of the returned conversion service allows for
the convenient addition and removal of individual Converter instances:
ConfigurableConversionService cs = env.getConversionService(); cs.addConverter(new FooConverter());
PropertyResolver.getProperty(String, Class),
ConverterRegistry.addConverter(org.springframework.core.convert.converter.Converter, ?>)public void setPlaceholderPrefix(String placeholderPrefix)
ConfigurablePropertyResolver
setPlaceholderPrefix in interface ConfigurablePropertyResolverpublic void setPlaceholderSuffix(String placeholderSuffix)
ConfigurablePropertyResolver
setPlaceholderSuffix in interface ConfigurablePropertyResolverpublic void setValueSeparator(String valueSeparator)
ConfigurablePropertyResolvernull if no such
special character should be processed as a value separator.
setValueSeparator in interface ConfigurablePropertyResolverpublic String toString()
toString in class Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||