Package org.springframework.core.env

# Class CompositePropertySource

java.lang.Object
org.springframework.core.env.PropertySource<T>
org.springframework.core.env.EnumerablePropertySource<Object>
org.springframework.core.env.CompositePropertySource

public class CompositePropertySource extends EnumerablePropertySource<Object>
Composite `PropertySource` implementation that iterates over a set of `PropertySource` instances. Necessary in cases where multiple property sources share the same name, for example, when multiple values are supplied to `@PropertySource`.

As of Spring 4.1.2, this class extends `EnumerablePropertySource` instead of plain `PropertySource`, exposing `getPropertyNames()` based on the accumulated property names from all contained sources - and failing with an `IllegalStateException` against any non-`EnumerablePropertySource`. When used through the `EnumerablePropertySource` contract, all contained sources are expected to be of type `EnumerablePropertySource` as well.

Since:
  3.1.1
Author:
  Chris Beams, Juergen Hoeller, Phillip Webb

## 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.PropertySource

`logger, name, source`

## Constructor Summary

Constructors
Constructor
Description
`CompositePropertySource(String name)`
Create a new `CompositePropertySource`.

## Method Summary

Modifier and Type
Method
Description
`void`
`addFirstPropertySource(PropertySource<?> propertySource)`
Add the given `PropertySource` to the start of the chain.
`void`
`addPropertySource(PropertySource<?> propertySource)`
Add the given `PropertySource` to the end of the chain.
`boolean`
`containsProperty(String name)`
Return whether this `PropertySource` contains a property with the given name.
`Object`
`getProperty(String name)`
Return the value associated with the given name, or `null` if not found.
`String[]`
`getPropertyNames()`
Return the names of all properties contained by the source object (never `null`).
`Collection<PropertySource<?>>`
`getPropertySources()`
Return all property sources that this composite source holds.
`String`
`toString()`
Produce concise output (type and name) if the current log level does not include debug.

### Methods inherited from class org.springframework.core.env.PropertySource

`equals, getName, getSource, hashCode, named`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### CompositePropertySource

public CompositePropertySource(String name)
Create a new `CompositePropertySource`.

Parameters:
  `name` - the name of the property source

## Method Details

### getProperty

@Nullable public Object getProperty(String name)
Description copied from class: `PropertySource`
Return the value associated with the given name, or `null` if not found.

Specified by:
  `getProperty` in class `PropertySource<Object>`
Parameters:
  `name` - the property to find
See Also:
  - `PropertyResolver.getRequiredProperty(String)`

### containsProperty

public boolean containsProperty(String name)
Description copied from class: `EnumerablePropertySource`
Return whether this `PropertySource` contains a property with the given name.

This implementation checks for the presence of the given name within the `EnumerablePropertySource.getPropertyNames()` array.

Overrides:
  `containsProperty` in class `EnumerablePropertySource<Object>`
Parameters:
  `name` - the name of the property to find

### getPropertyNames

public String[] getPropertyNames()
Description copied from class: `EnumerablePropertySource`
Return the names of all properties contained by the source object (never `null`).

Specified by:
  `getPropertyNames` in class `EnumerablePropertySource<Object>`

### addPropertySource

public void addPropertySource(PropertySource<?> propertySource)
Add the given `PropertySource` to the end of the chain.

Parameters:
  `propertySource` - the PropertySource to add

### addFirstPropertySource

public void addFirstPropertySource(PropertySource<?> propertySource)
Add the given `PropertySource` to the start of the chain.

Parameters:
  `propertySource` - the PropertySource to add
Since:
  4.1

### getPropertySources

public Collection<PropertySource<?>> getPropertySources()
Return all property sources that this composite source holds.

Since:
  4.1.1

### toString

public String toString()
Description copied from class: `PropertySource`
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.

Overrides:
  `toString` in class `PropertySource<Object>`
See Also:
  - `Log.isDebugEnabled()`