public class

DefaultEnvironment

extends AbstractEnvironment
java.lang.Object
   ↳ org.springframework.core.env.AbstractEnvironment
     ↳ org.springframework.core.env.DefaultEnvironment
Known Direct Subclasses

Class Overview

Default implementation of the Environment interface. Used throughout all non-Web* ApplicationContext implementations.

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:

  1. system properties
  2. system environment variables
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.getPropertyResolver().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 getPropertySources().

Example: adding a new property source with highest search priority

   ConfigurableEnvironment environment = new DefaultEnvironment();
   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(DefaultEnvironment.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(DefaultEnvironment.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.

Summary

Constants
String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME System environment property source name: {@value }
String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME JVM system properties property source name: {@value }
[Expand]
Inherited Constants
From class org.springframework.core.env.AbstractEnvironment
[Expand]
Inherited Fields
From class org.springframework.core.env.AbstractEnvironment
Public Constructors
DefaultEnvironment()
Create a new Environment populated with property sources in the following order:
  • {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME}
  • {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}

Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}.

[Expand]
Inherited Methods
From class org.springframework.core.env.AbstractEnvironment
From class java.lang.Object
From interface org.springframework.core.env.ConfigurableEnvironment
From interface org.springframework.core.env.ConfigurablePropertyResolver
From interface org.springframework.core.env.Environment
From interface org.springframework.core.env.PropertyResolver

Constants

public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME

Also: SpringCore

System environment property source name: {@value }

Constant Value: "systemEnvironment"

public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME

Also: SpringCore

JVM system properties property source name: {@value }

Constant Value: "systemProperties"

Public Constructors

public DefaultEnvironment ()

Also: SpringCore

Create a new Environment populated with property sources in the following order:

  • {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME}
  • {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}

Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}.