Class PropertySource<T>
- java.lang.Object
- org.springframework.core.env.PropertySource<T>
- Direct Known Subclasses:
EnumerablePropertySource,JndiPropertySource,PropertySource.StubPropertySource
public abstract class PropertySource<T> extends Object
Abstract base class representing a source of name/value property pairs. The underlying source object may be of any typeTthat encapsulates properties. Examples includePropertiesobjects,Mapobjects,ServletContextandServletConfigobjects (for access to init parameters). Explore thePropertySourcetype hierarchy to see provided implementations.PropertySourceobjects are not typically used in isolation, but rather through aPropertySourcesobject, which aggregates property sources and in conjunction with aPropertyResolverimplementation that can perform precedence-based searches across the set ofPropertySources.PropertySourceidentity is determined not based on the content of encapsulated properties, but rather based on thenameof thePropertySourcealone. This is useful for manipulatingPropertySourceobjects when in collection contexts. See operations inMutablePropertySourcesas well as thenamed(String)andtoString()methods for details.Note that when working with @
Configurationclasses that the @PropertySourceannotation provides a convenient and declarative way of adding property sources to the enclosingEnvironment.- Since:
- 3.1
- Author:
- Chris Beams
- See Also:
PropertySources,PropertyResolver,PropertySourcesPropertyResolver,MutablePropertySources,PropertySource
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPropertySource.StubPropertySourcePropertySourceto be used as a placeholder in cases where an actual property source cannot be eagerly initialized at application context creation time.
Constructor Summary
Constructors Constructor Description PropertySource(String name)Create a newPropertySourcewith the given name and with a newObjectinstance as the underlying source.PropertySource(String name, T source)Create a newPropertySourcewith the given name and source object.
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleancontainsProperty(String name)Return whether thisPropertySourcecontains the given name.booleanequals(Object obj)ThisPropertySourceobject is equal to the given object if: they are the same instance thenameproperties for both objects are equalStringgetName()Return the name of thisPropertySourceabstract ObjectgetProperty(String name)Return the value associated with the given name, ornullif not found.TgetSource()Return the underlying source object for thisPropertySource.inthashCode()Return a hash code derived from thenameproperty of thisPropertySourceobject.static PropertySource<?>named(String name)Return aPropertySourceimplementation intended for collection comparison purposes only.StringtoString()Produce concise output (type and name) if the current log level does not include debug.
Constructor Detail
PropertySource
public PropertySource(String name, T source)
Create a newPropertySourcewith the given name and source object.
PropertySource
public PropertySource(String name)
Create a newPropertySourcewith the given name and with a newObjectinstance 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.
Method Detail
containsProperty
public boolean containsProperty(String name)
Return whether thisPropertySourcecontains the given name.This implementation simply checks for a
nullreturn value fromgetProperty(String). Subclasses may wish to implement a more efficient algorithm if possible.- Parameters:
name- the property name to find
getProperty
public abstract Object getProperty(String name)
Return the value associated with the given name, ornullif not found.- Parameters:
name- the property to find- See Also:
PropertyResolver.getRequiredProperty(String)
equals
public boolean equals(Object obj)
ThisPropertySourceobject is equal to the given object if:- they are the same instance
- the
nameproperties for both objects are equal
No properties other than
nameare evaluated.
hashCode
public int hashCode()
Return a hash code derived from thenameproperty of thisPropertySourceobject.
toString
public String 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 difficult to read exception and log messages.
- Overrides:
toStringin classObject- See Also:
Log.isDebugEnabled()
named
public static PropertySource<?> named(String name)
Return aPropertySourceimplementation intended for collection comparison purposes only.Primarily for internal use, but given a collection of
PropertySourceobjects, may be used as follows:
The returnedList<PropertySource<?>> sources = new ArrayList<PropertySource<?>>(); 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"));PropertySourcewill throwUnsupportedOperationExceptionif any methods other thanequals(Object),hashCode(), andtoString()are called.- Parameters:
name- the name of the comparisonPropertySourceto be created and returned.