org.springframework.core.env
Class StandardEnvironment

java.lang.Object
  extended by org.springframework.core.env.AbstractEnvironment
      extended by org.springframework.core.env.StandardEnvironment
All Implemented Interfaces:
ConfigurableEnvironment, ConfigurablePropertyResolver, Environment, PropertyResolver
Direct Known Subclasses:
StandardPortletEnvironment, StandardServletEnvironment

public class StandardEnvironment
extends AbstractEnvironment

Environment implementation suitable for use in 'standard' (i.e. non-web) applications.

In addition to the usual functions of a ConfigurableEnvironment such as property resolution and profile-related operations, this implementation configures two default property sources, to be searched in the following order:

That is, if the key "xyz" is present both in the JVM system properties as well as in the set of environment variables for the current process, the value of key "xyz" from system properties * will return from a call to environment.getProperty("xyz"). This ordering is chosen by default because system properties are per-JVM, while environment variables may be the same across many JVMs on a given system. Giving system properties precedence allows for overriding of environment variables on a per-JVM basis.

These default property sources may be removed, reordered, or replaced; and additional property sources may be added using the MutablePropertySources instance available from AbstractEnvironment.getPropertySources().

Example: adding a new property source with highest search priority

   ConfigurableEnvironment environment = new StandardEnvironment();
   MutablePropertySources propertySources = environment.getPropertySources();
   Map 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 PropertySources are available during the container bootstrap process, including use by property placeholder configurers.

Since:
3.1
Author:
Chris Beams
See Also:
ConfigurableEnvironment, StandardServletEnvironment

Field Summary
static String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
          System environment property source name: "systemEnvironment"
static String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME
          JVM system properties property source name: "systemProperties"
 
Fields inherited from class org.springframework.core.env.AbstractEnvironment
ACTIVE_PROFILES_PROPERTY_NAME, DEFAULT_PROFILES_PROPERTY_NAME, logger, RESERVED_DEFAULT_PROFILE_NAME
 
Constructor Summary
StandardEnvironment()
           
 
Method Summary
protected  void customizePropertySources(MutablePropertySources propertySources)
          Customize the set of property sources with those appropriate for any standard Java environment: "systemProperties" "systemEnvironment"
 
Methods inherited from class org.springframework.core.env.AbstractEnvironment
acceptsProfiles, containsProperty, doGetActiveProfiles, doGetDefaultProfiles, getActiveProfiles, getConversionService, getDefaultProfiles, getProperty, getProperty, getProperty, getProperty, getPropertyAsClass, getPropertySources, getRequiredProperty, getRequiredProperty, getReservedDefaultProfiles, getSystemEnvironment, getSystemProperties, resolvePlaceholders, resolveRequiredPlaceholders, setActiveProfiles, setConversionService, setDefaultProfiles, setPlaceholderPrefix, setPlaceholderSuffix, setRequiredProperties, setValueSeparator, toString, validateRequiredProperties
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME

public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
System environment property source name: "systemEnvironment"

See Also:
Constant Field Values

SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME

public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME
JVM system properties property source name: "systemProperties"

See Also:
Constant Field Values
Constructor Detail

StandardEnvironment

public StandardEnvironment()
Method Detail

customizePropertySources

protected void customizePropertySources(MutablePropertySources propertySources)
Customize the set of property sources with those appropriate for any standard Java environment:

Properties present in "systemProperties" will take precedence over those in "systemEnvironment".

Overrides:
customizePropertySources in class AbstractEnvironment
See Also:
AbstractEnvironment.customizePropertySources(MutablePropertySources), AbstractEnvironment.getSystemProperties(), AbstractEnvironment.getSystemEnvironment()