Spring Framework

org.springframework.core.env
Class SystemEnvironmentPropertySource

java.lang.Object
  extended by org.springframework.core.env.PropertySource<T>
      extended by org.springframework.core.env.EnumerablePropertySource<java.util.Map<java.lang.String,java.lang.Object>>
          extended by org.springframework.core.env.MapPropertySource
              extended by org.springframework.core.env.SystemEnvironmentPropertySource

public class SystemEnvironmentPropertySource
extends MapPropertySource

Specialization of MapPropertySource designed for use with system environment variables. Compensates for constraints in Bash and other shells that do not allow for variables containing the period character; also allows for uppercase variations on property names for more idiomatic shell use.

For example, a call to getProperty("foo.bar") will attempt to find a value for the original property or any 'equivalent' property, returning the first found:

The same applies for calls to containsProperty(String), which returns true if any of the above properties are present, otherwise false.

This feature is particularly useful when specifying active or default profiles as environment variables. The following is not allowable under Bash

spring.profiles.active=p1 java -classpath ... MyApp
However, the following syntax is permitted and is also more conventional.
SPRING_PROFILES_ACTIVE=p1 java -classpath ... MyApp

Enable debug- or trace-level logging for this class (or package) for messages explaining when these 'property name resolutions' occur.

This property source is included by default in StandardEnvironment and all its subclasses.

Since:
3.1
Author:
Chris Beams
See Also:
StandardEnvironment, AbstractEnvironment.getSystemEnvironment(), AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME

Nested Class Summary
 
Nested classes/interfaces inherited from class org.springframework.core.env.PropertySource
PropertySource.StubPropertySource
 
Field Summary
 
Fields inherited from class org.springframework.core.env.EnumerablePropertySource
EMPTY_NAMES_ARRAY, logger
 
Fields inherited from class org.springframework.core.env.PropertySource
name, source
 
Constructor Summary
SystemEnvironmentPropertySource(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> source)
          Create a new SystemEnvironmentPropertySource with the given name and delegating to the given MapPropertySource.
 
Method Summary
 boolean containsProperty(java.lang.String name)
          Return true if a property with the given name or any underscore/uppercase variant thereof exists in this property source.
 java.lang.Object getProperty(java.lang.String name)
          Return the value associated with the given name, null if not found.
 
Methods inherited from class org.springframework.core.env.MapPropertySource
getPropertyNames
 
Methods inherited from class org.springframework.core.env.PropertySource
equals, getName, getSource, hashCode, named, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SystemEnvironmentPropertySource

public SystemEnvironmentPropertySource(java.lang.String name,
                                       java.util.Map<java.lang.String,java.lang.Object> source)
Create a new SystemEnvironmentPropertySource with the given name and delegating to the given MapPropertySource.

Method Detail

containsProperty

public boolean containsProperty(java.lang.String name)
Return true if a property with the given name or any underscore/uppercase variant thereof exists in this property source.

Overrides:
containsProperty in class EnumerablePropertySource<java.util.Map<java.lang.String,java.lang.Object>>
Parameters:
name - the property to find

getProperty

public java.lang.Object getProperty(java.lang.String name)
Return the value associated with the given name, null if not found.

This implementation returns true if a property with the given name or any underscore/uppercase variant thereof exists in this property source.

Overrides:
getProperty in class MapPropertySource
Parameters:
name - the property to find
See Also:
PropertyResolver.getRequiredProperty(String)

Spring Framework