接口 ConfigurableEnvironment

  • 所有超级接口:
    ConfigurablePropertyResolver, Environment, PropertyResolver
    所有已知子接口:
    ConfigurableWebEnvironment
    所有已知实现类:
    AbstractEnvironment, MockEnvironment, StandardEnvironment, StandardPortletEnvironment, StandardServletEnvironment

    public interface ConfigurableEnvironment
    extends Environment, ConfigurablePropertyResolver
    Configuration interface to be implemented by most if not all 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.

    Manipulating property sources

    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.

    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 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.
    从以下版本开始:
    3.1
    作者:
    Chris Beams
    另请参阅:
    StandardEnvironment, ConfigurableApplicationContext.getEnvironment()