17
17
package org .springframework .core .env ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
20
21
import java .util .Collection ;
21
22
import java .util .LinkedHashSet ;
22
23
import java .util .List ;
23
24
import java .util .Set ;
24
25
26
+ import org .springframework .util .StringUtils ;
27
+
25
28
/**
26
29
* Composite {@link PropertySource} implementation that iterates over a set of
27
30
* {@link PropertySource} instances. Necessary in cases where multiple property sources
28
31
* share the same name, e.g. when multiple values are supplied to {@code @PropertySource}.
29
32
*
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
+ *
30
37
* @author Chris Beams
38
+ * @author Juergen Hoeller
31
39
* @author Phillip Webb
32
40
* @since 3.1.1
33
41
*/
34
- public class CompositePropertySource extends PropertySource <Object > {
42
+ public class CompositePropertySource extends EnumerablePropertySource <Object > {
35
43
36
44
private final Set <PropertySource <?>> propertySources = new LinkedHashSet <PropertySource <?>>();
37
45
@@ -56,6 +64,28 @@ public Object getProperty(String name) {
56
64
return null ;
57
65
}
58
66
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
+
59
89
/**
60
90
* Add the given {@link PropertySource} to the end of the chain.
61
91
* @param propertySource the PropertySource to add
@@ -84,6 +114,7 @@ public Collection<PropertySource<?>> getPropertySources() {
84
114
return this .propertySources ;
85
115
}
86
116
117
+
87
118
@ Override
88
119
public String toString () {
89
120
return String .format ("%s [name='%s', propertySources=%s]" ,
0 commit comments