Top Description Inners Fields Constructors Methods
org.springframework.core.env

public abstract Class PropertySource<T>

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.core.env.PropertySource.StubPropertySource, org.springframework.core.env.EnumerablePropertySource
Type Parameters
<T>
the source type
Imports
java.util.Objects, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils

Abstract base class representing a source of name/value property pairs. The underlying source object may be of any type T that encapsulates properties. Examples include java.util.Properties objects, java.util.Map objects, ServletContext and ServletConfig objects (for access to init parameters). Explore the PropertySource type hierarchy to see provided implementations.

PropertySource objects are not typically used in isolation, but rather through a PropertySources object, which aggregates property sources and in conjunction with a PropertyResolver implementation that can perform precedence-based searches across the set of PropertySources.

PropertySource identity is determined not based on the content of encapsulated properties, but rather based on the name of the PropertySource alone. This is useful for manipulating PropertySource objects when in collection contexts. See operations in MutablePropertySources as well as the named(String) and toString() methods for details.

Note that when working with @Configuration classes that the @PropertySource annotation provides a convenient and declarative way of adding property sources to the enclosing Environment.

Author
Chris Beams
Since
3.1
See Also
PropertySources, PropertyResolver, PropertySourcesPropertyResolver, MutablePropertySources, org.springframework.context.annotation.PropertySource

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
PropertySource.ComparisonPropertySource

A PropertySource implementation intended for collection comparison purposes.

public static class
PropertySource.StubPropertySource

PropertySource to be used as a placeholder in cases where an actual property source cannot be eagerly initialized at application context creation time.

Field Summary

Modifier and TypeField and Description
protected final Log
protected final String
protected final T

Constructor Summary

AccessConstructor and Description
public
PropertySource(String
the associated name
name
,
T
the source object
source
)

Create a new PropertySource with the given name and source object.

public
PropertySource(String name)

Create a new PropertySource with the given name and with a new Object instance as the underlying source.

Method Summary

Modifier and TypeMethod and Description
public boolean
containsProperty(String
the property name to find
name
)

Return whether this PropertySource contains the given name.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

This PropertySource object is equal to the given object if:
  • they are the same instance
  • the name properties for both objects are equal
public String
getName()

Return the name of this PropertySource.

public abstract Object
getProperty(String
the property to find
name
)

Return the value associated with the given name, or null if not found.

public T
getSource()

Return the underlying source object for this PropertySource.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Return a hash code derived from the name property of this PropertySource object.
public static PropertySource<?>
named(String
the name of the comparison PropertySource to be created and returned
name
)

Return a PropertySource implementation intended for collection comparison purposes only.

public String
toString()

Overrides java.lang.Object.toString.

Produce concise output (type and name) if the current log level does not include debug.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

loggerback to summary
protected final Log logger
nameback to summary
protected final String name
sourceback to summary
protected final T source

Constructor Detail

PropertySourceback to summary
public PropertySource(String name, T source)

Create a new PropertySource with the given name and source object.

Parameters
name:String

the associated name

source:T

the source object

PropertySourceback to summary
public PropertySource(String name)

Create a new PropertySource with the given name and with a new Object instance as the underlying source.

Often useful in testing scenarios when creating anonymous implementations that never query an actual source but rather return hard-coded values.

Annotations
@SuppressWarnings:unchecked

Method Detail

containsPropertyback to summary
public boolean containsProperty(String name)

Return whether this PropertySource contains the given name.

This implementation simply checks for a null return value from getProperty(String). Subclasses may wish to implement a more efficient algorithm if possible.

Parameters
name:String

the property name to find

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

This PropertySource object is equal to the given object if:

  • they are the same instance
  • the name properties for both objects are equal

No properties other than name are evaluated.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getNameback to summary
public String getName()

Return the name of this PropertySource.

See the class-level Javadoc for details on property source identity and names.

getPropertyback to summary
public abstract Object getProperty(String name)

Return the value associated with the given name, or null if not found.

Parameters
name:String

the property to find

Annotations
@Nullable
See Also
PropertyResolver#getRequiredProperty(String)
getSourceback to summary
public T getSource()

Return the underlying source object for this PropertySource.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Return a hash code derived from the name property of this PropertySource object.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
namedback to summary
public static PropertySource<?> named(String name)

Return a PropertySource implementation intended for collection comparison purposes only.

Primarily for internal use, but given a collection of PropertySource objects, may be used as follows:

List<PropertySource<?>> sources = new ArrayList<>();
sources.add(new MapPropertySource("sourceA", mapA));
sources.add(new MapPropertySource("sourceB", mapB));
assert sources.contains(PropertySource.named("sourceA"));
assert sources.contains(PropertySource.named("sourceB"));
assert !sources.contains(PropertySource.named("sourceC"));

The returned PropertySource will throw UnsupportedOperationException if any methods other than equals(Object), hashCode(), and toString() are called.

Parameters
name:String

the name of the comparison PropertySource to be created and returned

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Produce concise output (type and name) if the current log level does not include debug. If debug is enabled, produce verbose output including the hash code of the PropertySource instance and every name/value property pair.

This variable verbosity is useful as a property source such as system properties or environment variables may contain an arbitrary number of property pairs, potentially leading to difficulties to read exception and log messages.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
See Also
Log#isDebugEnabled()
org.springframework.core.env back to summary

pack-priv Class PropertySource.ComparisonPropertySource

extends StubPropertySource
Class Inheritance

A PropertySource implementation intended for collection comparison purposes.
See Also
PropertySource#named(String)

Field Summary

Modifier and TypeField and Description
private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
containsProperty(String
the property name to find
name
)

Overrides org.springframework.core.env.PropertySource.containsProperty.

Return whether this PropertySource contains the given name.
public String
public Object
getSource()

Overrides org.springframework.core.env.PropertySource.getSource.

Return the underlying source object for this PropertySource.

Field Detail

USAGE_ERRORback to summary
private static final String USAGE_ERROR

Constructor Detail

ComparisonPropertySourceback to summary
public ComparisonPropertySource(String name)

Method Detail

containsPropertyback to summary
public boolean containsProperty(String name)

Overrides org.springframework.core.env.PropertySource.containsProperty.

Doc from org.springframework.core.env.PropertySource.containsProperty.

Return whether this PropertySource contains the given name.

This implementation simply checks for a null return value from getProperty(String). Subclasses may wish to implement a more efficient algorithm if possible.

Parameters
name:String

the property name to find

Annotations
@Override
getPropertyback to summary
public String getProperty(String name)

Overrides org.springframework.core.env.PropertySource.StubPropertySource.getProperty.

Doc from org.springframework.core.env.PropertySource.StubPropertySource.getProperty.

Always returns null.

Parameters
name:String

Doc from org.springframework.core.env.PropertySource.getProperty.

the property to find

Annotations
@Override
@Nullable
getSourceback to summary
public Object getSource()

Overrides org.springframework.core.env.PropertySource.getSource.

Doc from org.springframework.core.env.PropertySource.getSource.

Return the underlying source object for this PropertySource.

Annotations
@Override
org.springframework.core.env back to summary

public Class PropertySource.StubPropertySource

extends PropertySource<Object>
Class Inheritance
Known Direct Subclasses
org.springframework.core.env.PropertySource.ComparisonPropertySource

PropertySource to be used as a placeholder in cases where an actual property source cannot be eagerly initialized at application context creation time. For example, a ServletContext-based property source must wait until the ServletContext object is available to its enclosing ApplicationContext. In such cases, a stub should be used to hold the intended default position/order of the property source, then be replaced during context refresh.
See Also
org.springframework.context.support.AbstractApplicationContext#initPropertySources(), org.springframework.web.context.support.StandardServletEnvironment, org.springframework.web.context.support.ServletContextPropertySource

Field Summary

Inherited from org.springframework.core.env.PropertySource:
loggernamesource

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
getProperty(String
the property to find
name
)

Implements abstract org.springframework.core.env.PropertySource.getProperty.

Always returns null.
Inherited from org.springframework.core.env.PropertySource:
containsPropertyequalsgetNamegetSourcehashCodenamedtoString

Constructor Detail

StubPropertySourceback to summary
public StubPropertySource(String name)

Method Detail

getPropertyback to summary
public String getProperty(String name)

Implements abstract org.springframework.core.env.PropertySource.getProperty.

Always returns null.

Parameters
name:String

Doc from org.springframework.core.env.PropertySource.getProperty.

the property to find

Annotations
@Override
@Nullable