类 PropertySource<T>
- java.lang.Object
- org.springframework.core.env.PropertySource<T>
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 typeT
that encapsulates properties. Examples includeProperties
objects,Map
objects,ServletContext
andServletConfig
objects (for access to init parameters). Explore thePropertySource
type hierarchy to see provided implementations.PropertySource
objects are not typically used in isolation, but rather through aPropertySources
object, which aggregates property sources and in conjunction with aPropertyResolver
implementation that can perform precedence-based searches across the set ofPropertySources
.PropertySource
identity is determined not based on the content of encapsulated properties, but rather based on thename
of thePropertySource
alone. This is useful for manipulatingPropertySource
objects when in collection contexts. See operations inMutablePropertySources
as well as thenamed(String)
andtoString()
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 enclosingEnvironment
.- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams
- 另请参阅:
PropertySources
,PropertyResolver
,PropertySourcesPropertyResolver
,MutablePropertySources
,PropertySource
嵌套类概要
嵌套类 修饰符和类型 类 说明 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.
构造器概要
构造器 构造器 说明 PropertySource(String name)
Create a newPropertySource
with the given name and with a newObject
instance as the underlying source.PropertySource(String name, T source)
Create a newPropertySource
with the given name and source object.
方法概要
所有方法 静态方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 boolean
containsProperty(String name)
Return whether thisPropertySource
contains the given name.boolean
equals(Object obj)
ThisPropertySource
object is equal to the given object if: they are the same instance thename
properties for both objects are equalString
getName()
Return the name of thisPropertySource
abstract Object
getProperty(String name)
Return the value associated with the given name, ornull
if not found.T
getSource()
Return the underlying source object for thisPropertySource
.int
hashCode()
Return a hash code derived from thename
property of thisPropertySource
object.static PropertySource<?>
named(String name)
Return aPropertySource
implementation intended for collection comparison purposes only.String
toString()
Produce concise output (type and name) if the current log level does not include debug.
构造器详细资料
PropertySource
public PropertySource(String name, T source)
Create a newPropertySource
with the given name and source object.
PropertySource
public PropertySource(String name)
Create a newPropertySource
with the given name and with a newObject
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.
方法详细资料
containsProperty
public boolean containsProperty(String name)
Return whether thisPropertySource
contains the given name.This implementation simply checks for a
null
return value fromgetProperty(String)
. Subclasses may wish to implement a more efficient algorithm if possible.- 参数:
name
- the property name to find
getProperty
public abstract Object getProperty(String name)
Return the value associated with the given name, ornull
if not found.- 参数:
name
- the property to find- 另请参阅:
PropertyResolver.getRequiredProperty(String)
equals
public boolean equals(Object obj)
ThisPropertySource
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.
hashCode
public int hashCode()
Return a hash code derived from thename
property of thisPropertySource
object.
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.
- 覆盖:
toString
在类中Object
- 另请参阅:
Log.isDebugEnabled()
named
public static PropertySource<?> named(String name)
Return aPropertySource
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<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"));
PropertySource
will throwUnsupportedOperationException
if any methods other thanequals(Object)
,hashCode()
, andtoString()
are called.- 参数:
name
- the name of the comparisonPropertySource
to be created and returned.