Environment 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 the ConfigurablePropertyResolver
superinterface.
Property sources may be removed, reordered, or replaced; and additional
property sources may be added using the MutablePropertySources
instance returned from getPropertySources(). The following examples
are against the StandardEnvironment implementation of
ConfigurableEnvironment, but are generally applicable to any implementation,
though particular default property sources may differ.
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> myMap = new HashMap<>();
myMap.put("xyz", "myValue");
propertySources.addFirst(new MapPropertySource("MY_MAP", myMap));
MutablePropertySources propertySources = environment.getPropertySources(); propertySources.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)
MutablePropertySources propertySources = environment.getPropertySources();
MockPropertySource mockEnvVars = new MockPropertySource().withProperty("xyz", "myValue");
propertySources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, mockEnvVars);
When an Environment is being used by an ApplicationContext, it is
important that any such PropertySource manipulations be performed
before the context's refresh()
method is called. This ensures that all property sources are available during the
container bootstrap process, including use by property
placeholder configurers.
StandardEnvironment, org.springframework.context.ConfigurableApplicationContext#getEnvironment
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public MutablePropertySources | getPropertySources()
Return the |
| public Map | |
| public Map | |
| public void | merge(ConfigurableEnvironment
the environment to merge with parent)Append the given parent environment's active profiles, default profiles and property sources to this (child) environment's respective collections of each. |
| public void | |
| public void | setDefaultProfiles(String... profiles)
Specify the set of profiles to be made active by default if no other profiles
are explicitly made active through |
| addActiveProfile | back to summary |
|---|---|
| public void addActiveProfile(String profile) Add a profile to the current set of active profiles.
| |
| getPropertySources | back to summary |
|---|---|
| public MutablePropertySources getPropertySources() Return the | |
| getSystemEnvironment | back to summary |
|---|---|
| public Map Return the value of Note that most | |
| getSystemProperties | back to summary |
|---|---|
| public Map Return the value of Note that most | |
| merge | back to summary |
|---|---|
| public 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 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
| |
| setActiveProfiles | back to summary |
|---|---|
| public void setActiveProfiles(String... profiles) 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
| |
| setDefaultProfiles | back to summary |
|---|---|
| public void setDefaultProfiles(String... profiles) Specify the set of profiles to be made active by default if no other profiles
are explicitly made active through
| |