Property sources may be removed, reordered, or replaced; and additional property sources may be added using the {@link MutablePropertySources}instance returned from {@link #getPropertySources()}. The following examples are against the {@link StandardEnvironment} implementation of{@code ConfigurableEnvironment}, but are generally applicable to any implementation, though particular default property sources may differ.
ConfigurableEnvironment environment = new StandardEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); MapmyMap = 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 {@link Environment} is being used by an {@code ApplicationContext}, it is important that any such {@code PropertySource} manipulations be performedbefore the context's {@link org.springframework.context.support.AbstractApplicationContext#refresh() refresh()}method is called. This ensures that all property sources are available during the container bootstrap process, including use by {@linkplain org.springframework.context.support.PropertySourcesPlaceholderConfigurer propertyplaceholder configurers}. @author Chris Beams @since 3.1 @see StandardEnvironment @see org.springframework.context.ConfigurableApplicationContext#getEnvironment
|
|