Skip to content

Commit 9d96958

Browse files
committed
CompositePropertySource extends EnumerablePropertySource now
Issue: SPR-12292
1 parent fd69ee5 commit 9d96958

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,29 @@
1717
package org.springframework.core.env;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
2324
import java.util.Set;
2425

26+
import org.springframework.util.StringUtils;
27+
2528
/**
2629
* Composite {@link PropertySource} implementation that iterates over a set of
2730
* {@link PropertySource} instances. Necessary in cases where multiple property sources
2831
* share the same name, e.g. when multiple values are supplied to {@code @PropertySource}.
2932
*
33+
* <p>As of Spring 4.1.2, this class extends {@link EnumerablePropertySource} instead
34+
* of plain {@link PropertySource}, exposing {@link #getPropertyNames()} based on the
35+
* accumulated property names from all contained sources (as far as possible).
36+
*
3037
* @author Chris Beams
38+
* @author Juergen Hoeller
3139
* @author Phillip Webb
3240
* @since 3.1.1
3341
*/
34-
public class CompositePropertySource extends PropertySource<Object> {
42+
public class CompositePropertySource extends EnumerablePropertySource<Object> {
3543

3644
private final Set<PropertySource<?>> propertySources = new LinkedHashSet<PropertySource<?>>();
3745

@@ -56,6 +64,28 @@ public Object getProperty(String name) {
5664
return null;
5765
}
5866

67+
@Override
68+
public boolean containsProperty(String name) {
69+
for (PropertySource<?> propertySource : this.propertySources) {
70+
if (propertySource.containsProperty(name)) {
71+
return true;
72+
}
73+
}
74+
return false;
75+
}
76+
77+
@Override
78+
public String[] getPropertyNames() {
79+
Set<String> names = new LinkedHashSet<String>();
80+
for (PropertySource<?> propertySource : this.propertySources) {
81+
if (propertySource instanceof EnumerablePropertySource) {
82+
names.addAll(Arrays.asList(((EnumerablePropertySource<?>) propertySource).getPropertyNames()));
83+
}
84+
}
85+
return StringUtils.toStringArray(names);
86+
}
87+
88+
5989
/**
6090
* Add the given {@link PropertySource} to the end of the chain.
6191
* @param propertySource the PropertySource to add
@@ -84,6 +114,7 @@ public Collection<PropertySource<?>> getPropertySources() {
84114
return this.propertySources;
85115
}
86116

117+
87118
@Override
88119
public String toString() {
89120
return String.format("%s [name='%s', propertySources=%s]",

spring-core/src/main/java/org/springframework/core/env/EnumerablePropertySource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public EnumerablePropertySource(String name, T source) {
4747
super(name, source);
4848
}
4949

50+
@SuppressWarnings("unchecked")
51+
protected EnumerablePropertySource(String name) {
52+
super(name);
53+
}
54+
5055

5156
/**
5257
* Return whether this {@code PropertySource} contains a property with the given name.

spring-core/src/main/java/org/springframework/core/env/PropertySource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public PropertySource(String name, T source) {
7575
}
7676

7777
/**
78-
* Create a new {@code PropertySource} with the given name and with a new {@code Object}
79-
* instance as the underlying source.
80-
* <p>Often useful in testing scenarios when creating anonymous implementations that
81-
* never query an actual source but rather return hard-coded values.
78+
* Create a new {@code PropertySource} with the given name and with a new
79+
* {@code Object} instance as the underlying source.
80+
* <p>Often useful in testing scenarios when creating anonymous implementations
81+
* that never query an actual source but rather return hard-coded values.
8282
*/
8383
@SuppressWarnings("unchecked")
8484
public PropertySource(String name) {

0 commit comments

Comments
 (0)