接口 ConfigurableEnvironment
- 所有已知子接口:
ConfigurableWebEnvironment
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver
Configuration interface to be implemented by most if not allEnvironment
types. Provides facilities for setting active and default profiles and manipulating underlying property sources. Allows clients to set and validate required properties, customize the conversion service and more through theConfigurablePropertyResolver
superinterface.Manipulating property sources
Property sources may be removed, reordered, or replaced; and additional property sources may be added using the
MutablePropertySources
instance returned fromgetPropertySources()
. The following examples are against theStandardEnvironment
implementation ofConfigurableEnvironment
, but are generally applicable to any implementation, though particular default property sources may differ.Example: adding a new property source with highest search priority
ConfigurableEnvironment environment = new StandardEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); Map<String, String> myMap = new HashMap<>(); myMap.put("xyz", "myValue"); propertySources.addFirst(new MapPropertySource("MY_MAP", myMap));
Example: removing the default system properties property source
MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)
Example: mocking the system environment for testing purposes
MutablePropertySources propertySources = environment.getPropertySources(); MockPropertySource mockEnvVars = new MockPropertySource().withProperty("xyz", "myValue"); propertySources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, mockEnvVars);
When anEnvironment
is being used by anApplicationContext
, it is important that any suchPropertySource
manipulations be performed before the context'srefresh()
method is called. This ensures that all property sources are available during the container bootstrap process, including use by property placeholder configurers.- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams
- 另请参阅:
StandardEnvironment
,ConfigurableApplicationContext.getEnvironment()
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 void
addActiveProfile(String profile)
Add a profile to the current set of active profiles.MutablePropertySources
getPropertySources()
Return thePropertySources
for thisEnvironment
in mutable form, allowing for manipulation of the set ofPropertySource
objects that should be searched when resolving properties against thisEnvironment
object.Map<String,Object>
getSystemEnvironment()
Return the value ofSystem.getenv()
if allowed by the currentSecurityManager
, otherwise return a map implementation that will attempt to access individual keys using calls toSystem.getenv(String)
.Map<String,Object>
getSystemProperties()
Return the value ofSystem.getProperties()
if allowed by the currentSecurityManager
, otherwise return a map implementation that will attempt to access individual keys using calls toSystem.getProperty(String)
.void
merge(ConfigurableEnvironment parent)
Append the given parent environment's active profiles, default profiles and property sources to this (child) environment's respective collections of each.void
setActiveProfiles(String... profiles)
Specify the set of profiles active for thisEnvironment
.void
setDefaultProfiles(String... profiles)
Specify the set of profiles to be made active by default if no other profiles are explicitly made active throughsetActiveProfiles(java.lang.String...)
.从接口继承的方法 org.springframework.core.env.ConfigurablePropertyResolver
getConversionService, setConversionService, setIgnoreUnresolvableNestedPlaceholders, setPlaceholderPrefix, setPlaceholderSuffix, setRequiredProperties, setValueSeparator, validateRequiredProperties
从接口继承的方法 org.springframework.core.env.Environment
acceptsProfiles, acceptsProfiles, getActiveProfiles, getDefaultProfiles
从接口继承的方法 org.springframework.core.env.PropertyResolver
containsProperty, getProperty, getProperty, getProperty, getProperty, getRequiredProperty, getRequiredProperty, resolvePlaceholders, resolveRequiredPlaceholders
方法详细资料
setActiveProfiles
void setActiveProfiles(String... profiles)
Specify the set of profiles active for thisEnvironment
. Profiles are evaluated during container bootstrap to determine whether bean definitions should be registered with the container.Any existing active profiles will be replaced with the given arguments; call with zero arguments to clear the current set of active profiles. Use
addActiveProfile(java.lang.String)
to add a profile while preserving the existing set.- 抛出:
IllegalArgumentException
- if any profile is null, empty or whitespace-only- 另请参阅:
addActiveProfile(java.lang.String)
,setDefaultProfiles(java.lang.String...)
,Profile
,AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME
addActiveProfile
void addActiveProfile(String profile)
Add a profile to the current set of active profiles.- 抛出:
IllegalArgumentException
- if the profile is null, empty or whitespace-only- 另请参阅:
setActiveProfiles(java.lang.String...)
setDefaultProfiles
void setDefaultProfiles(String... profiles)
Specify the set of profiles to be made active by default if no other profiles are explicitly made active throughsetActiveProfiles(java.lang.String...)
.- 抛出:
IllegalArgumentException
- if any profile is null, empty or whitespace-only- 另请参阅:
AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME
getPropertySources
MutablePropertySources getPropertySources()
Return thePropertySources
for thisEnvironment
in mutable form, allowing for manipulation of the set ofPropertySource
objects that should be searched when resolving properties against thisEnvironment
object. The variousMutablePropertySources
methods such asaddFirst
,addLast
,addBefore
andaddAfter
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.
getSystemProperties
Map<String,Object> getSystemProperties()
Return the value ofSystem.getProperties()
if allowed by the currentSecurityManager
, otherwise return a map implementation that will attempt to access individual keys using calls toSystem.getProperty(String)
.Note that most
Environment
implementations will include this system properties map as a defaultPropertySource
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 throwIllegalAccessException
; 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
Map<String,Object> getSystemEnvironment()
Return the value ofSystem.getenv()
if allowed by the currentSecurityManager
, otherwise return a map implementation that will attempt to access individual keys using calls toSystem.getenv(String)
.Note that most
Environment
implementations will include this system environment map as a defaultPropertySource
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 throwIllegalAccessException
; 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.
merge
void merge(ConfigurableEnvironment parent)
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
PropertySource
instance existing in both parent and child, the child instance is to be preserved and the parent instance discarded. This has the effect of allowing overriding of property sources by the child as well as avoiding redundant searches through common property source types, e.g. system environment and system properties.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
merge
will not be reflected in the child. Therefore, care should be taken to configure parent property sources and profile information prior to callingmerge
.- 参数:
parent
- the environment to merge with- 从以下版本开始:
- 3.1.2
- 另请参阅:
AbstractApplicationContext.setParent(org.springframework.context.ApplicationContext)