@@ -76,15 +76,15 @@ public void addArgumentValues(ConstructorArgumentValues other) {
76
76
}
77
77
for (ValueHolder valueHolder : other .genericArgumentValues ) {
78
78
if (!this .genericArgumentValues .contains (valueHolder )) {
79
- this . genericArgumentValues . add (valueHolder .copy ());
79
+ addOrMergeGenericArgumentValue (valueHolder .copy ());
80
80
}
81
81
}
82
82
}
83
83
}
84
84
85
85
86
86
/**
87
- * Add argument value for the given index in the constructor argument list.
87
+ * Add an argument value for the given index in the constructor argument list.
88
88
* @param index the index in the constructor argument list
89
89
* @param value the argument value
90
90
*/
@@ -93,7 +93,7 @@ public void addIndexedArgumentValue(int index, Object value) {
93
93
}
94
94
95
95
/**
96
- * Add argument value for the given index in the constructor argument list.
96
+ * Add an argument value for the given index in the constructor argument list.
97
97
* @param index the index in the constructor argument list
98
98
* @param value the argument value
99
99
* @param type the type of the constructor argument
@@ -103,7 +103,7 @@ public void addIndexedArgumentValue(int index, Object value, String type) {
103
103
}
104
104
105
105
/**
106
- * Add argument value for the given index in the constructor argument list.
106
+ * Add an argument value for the given index in the constructor argument list.
107
107
* @param index the index in the constructor argument list
108
108
* @param newValue the argument value in the form of a ValueHolder
109
109
*/
@@ -114,7 +114,7 @@ public void addIndexedArgumentValue(int index, ValueHolder newValue) {
114
114
}
115
115
116
116
/**
117
- * Add argument value for the given index in the constructor argument list,
117
+ * Add an argument value for the given index in the constructor argument list,
118
118
* merging the new value (typically a collection) with the current value
119
119
* if demanded: see {@link org.springframework.beans.Mergeable}.
120
120
* @param key the index in the constructor argument list
@@ -183,7 +183,7 @@ public Map<Integer, ValueHolder> getIndexedArgumentValues() {
183
183
184
184
185
185
/**
186
- * Add generic argument value to be matched by type.
186
+ * Add a generic argument value to be matched by type.
187
187
* <p>Note: A single generic argument value will just be used once,
188
188
* rather than matched multiple times.
189
189
* @param value the argument value
@@ -193,7 +193,7 @@ public void addGenericArgumentValue(Object value) {
193
193
}
194
194
195
195
/**
196
- * Add generic argument value to be matched by type.
196
+ * Add a generic argument value to be matched by type.
197
197
* <p>Note: A single generic argument value will just be used once,
198
198
* rather than matched multiple times.
199
199
* @param value the argument value
@@ -204,7 +204,7 @@ public void addGenericArgumentValue(Object value, String type) {
204
204
}
205
205
206
206
/**
207
- * Add generic argument value to be matched by type.
207
+ * Add a generic argument value to be matched by type or name (if available) .
208
208
* <p>Note: A single generic argument value will just be used once,
209
209
* rather than matched multiple times.
210
210
* @param newValue the argument value in the form of a ValueHolder
@@ -215,10 +215,33 @@ public void addGenericArgumentValue(Object value, String type) {
215
215
public void addGenericArgumentValue (ValueHolder newValue ) {
216
216
Assert .notNull (newValue , "ValueHolder must not be null" );
217
217
if (!this .genericArgumentValues .contains (newValue )) {
218
- this . genericArgumentValues . add (newValue );
218
+ addOrMergeGenericArgumentValue (newValue );
219
219
}
220
220
}
221
221
222
+ /**
223
+ * Add a generic argument value, merging the new value (typically a collection)
224
+ * with the current value if demanded: see {@link org.springframework.beans.Mergeable}.
225
+ * @param newValue the argument value in the form of a ValueHolder
226
+ */
227
+ private void addOrMergeGenericArgumentValue (ValueHolder newValue ) {
228
+ if (newValue .getName () != null ) {
229
+ for (Iterator <ValueHolder > it = this .genericArgumentValues .iterator (); it .hasNext ();) {
230
+ ValueHolder currentValue = it .next ();
231
+ if (newValue .getName ().equals (currentValue .getName ())) {
232
+ if (newValue .getValue () instanceof Mergeable ) {
233
+ Mergeable mergeable = (Mergeable ) newValue .getValue ();
234
+ if (mergeable .isMergeEnabled ()) {
235
+ newValue .setValue (mergeable .merge (currentValue .getValue ()));
236
+ }
237
+ }
238
+ it .remove ();
239
+ }
240
+ }
241
+ }
242
+ this .genericArgumentValues .add (newValue );
243
+ }
244
+
222
245
/**
223
246
* Look for a generic argument value that matches the given type.
224
247
* @param requiredType the type to match
@@ -244,8 +267,8 @@ public ValueHolder getGenericArgumentValue(Class requiredType, String requiredNa
244
267
* resolution process.
245
268
* @param requiredType the type to match (can be <code>null</code> to find
246
269
* an arbitrary next generic argument value)
247
- * @param requiredName the type to match (can be <code>null</code> to match
248
- * unnamed values only )
270
+ * @param requiredName the name to match (can be <code>null</code> to not
271
+ * match argument values by name )
249
272
* @param usedValueHolders a Set of ValueHolder objects that have already been used
250
273
* in the current resolution process and should therefore not be returned again
251
274
* @return the ValueHolder for the argument, or <code>null</code> if none found
0 commit comments