Skip to content

Commit a9c9ba6

Browse files
committed
Generic matching for ObjectProvider stream and empty vararg resolution
Issue: SPR-11419 Issue: SPR-15338
1 parent 6372c0f commit a9c9ba6

File tree

5 files changed

+143
-725
lines changed

5 files changed

+143
-725
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.beans.factory.support;
1818

1919
import java.beans.ConstructorProperties;
20+
import java.lang.reflect.Array;
2021
import java.lang.reflect.Constructor;
2122
import java.lang.reflect.Executable;
2223
import java.lang.reflect.Method;
@@ -824,9 +825,12 @@ protected Object resolveAutowiredArgument(MethodParameter param, String beanName
824825
}
825826
catch (NoSuchBeanDefinitionException ex) {
826827
if (fallback) {
827-
// Single constructor or factory method -> let's return an
828-
// empty collection for a non-null collection parameter.
829-
if (CollectionFactory.isApproximableCollectionType(paramType)) {
828+
// Single constructor or factory method -> let's return an empty array/collection
829+
// for e.g. a vararg or a non-null List/Set/Map parameter.
830+
if (paramType.isArray()) {
831+
return Array.newInstance(paramType.getComponentType(), 0);
832+
}
833+
else if (CollectionFactory.isApproximableCollectionType(paramType)) {
830834
return CollectionFactory.createCollection(paramType, 0);
831835
}
832836
else if (CollectionFactory.isApproximableMapType(paramType)) {

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ private Object resolveMultipleBeans(DependencyDescriptor descriptor, @Nullable S
12291229

12301230
if (descriptor.isStreamAccess()) {
12311231
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type,
1232-
new MultiElementDescriptor(descriptor));
1232+
new MultiElementDescriptor(descriptor, false));
12331233
if (autowiredBeanNames != null) {
12341234
autowiredBeanNames.addAll(matchingBeans.keySet());
12351235
}
@@ -1247,7 +1247,7 @@ else if (type.isArray()) {
12471247
return null;
12481248
}
12491249
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType,
1250-
new MultiElementDescriptor(descriptor));
1250+
new MultiElementDescriptor(descriptor, true));
12511251
if (matchingBeans.isEmpty()) {
12521252
return null;
12531253
}
@@ -1267,7 +1267,7 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
12671267
return null;
12681268
}
12691269
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
1270-
new MultiElementDescriptor(descriptor));
1270+
new MultiElementDescriptor(descriptor, true));
12711271
if (matchingBeans.isEmpty()) {
12721272
return null;
12731273
}
@@ -1292,7 +1292,7 @@ else if (Map.class == type) {
12921292
return null;
12931293
}
12941294
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, valueType,
1295-
new MultiElementDescriptor(descriptor));
1295+
new MultiElementDescriptor(descriptor, true));
12961296
if (matchingBeans.isEmpty()) {
12971297
return null;
12981298
}
@@ -1716,10 +1716,13 @@ public NestedDependencyDescriptor(DependencyDescriptor original) {
17161716
/**
17171717
* A dependency descriptor marker for multiple elements.
17181718
*/
1719-
private static class MultiElementDescriptor extends NestedDependencyDescriptor {
1719+
private static class MultiElementDescriptor extends DependencyDescriptor {
17201720

1721-
public MultiElementDescriptor(DependencyDescriptor original) {
1721+
public MultiElementDescriptor(DependencyDescriptor original, boolean nested) {
17221722
super(original);
1723+
if (nested) {
1724+
increaseNestingLevel();
1725+
}
17231726
}
17241727
}
17251728

0 commit comments

Comments
 (0)