Skip to content

Commit 4571975

Browse files
committed
Collection injection may refer back to factory methods on same bean again
Issue: SPR-14996 (cherry picked from commit 547b963)
1 parent 5115c61 commit 4571975

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ private Object resolveMultipleBeans(DependencyDescriptor descriptor, String bean
11471147
if (type.isArray()) {
11481148
Class<?> componentType = type.getComponentType();
11491149
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType,
1150-
new MultiElementDependencyDescriptor(descriptor));
1150+
new MultiElementDescriptor(descriptor));
11511151
if (matchingBeans.isEmpty()) {
11521152
return null;
11531153
}
@@ -1167,7 +1167,7 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
11671167
return null;
11681168
}
11691169
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
1170-
new MultiElementDependencyDescriptor(descriptor));
1170+
new MultiElementDescriptor(descriptor));
11711171
if (matchingBeans.isEmpty()) {
11721172
return null;
11731173
}
@@ -1191,7 +1191,7 @@ else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
11911191
return null;
11921192
}
11931193
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, valueType,
1194-
new MultiElementDependencyDescriptor(descriptor));
1194+
new MultiElementDescriptor(descriptor));
11951195
if (matchingBeans.isEmpty()) {
11961196
return null;
11971197
}
@@ -1258,25 +1258,27 @@ protected Map<String, Object> findAutowireCandidates(
12581258
}
12591259
}
12601260
}
1261-
for (String candidateName : candidateNames) {
1262-
if (!isSelfReference(beanName, candidateName) && isAutowireCandidate(candidateName, descriptor)) {
1263-
addCandidateEntry(result, candidateName, descriptor, requiredType);
1261+
for (String candidate : candidateNames) {
1262+
if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) {
1263+
addCandidateEntry(result, candidate, descriptor, requiredType);
12641264
}
12651265
}
12661266
if (result.isEmpty() && !indicatesMultipleBeans(requiredType)) {
12671267
// Consider fallback matches if the first pass failed to find anything...
12681268
DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch();
1269-
for (String candidateName : candidateNames) {
1270-
if (!isSelfReference(beanName, candidateName) && isAutowireCandidate(candidateName, fallbackDescriptor)) {
1271-
addCandidateEntry(result, candidateName, descriptor, requiredType);
1269+
for (String candidate : candidateNames) {
1270+
if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, fallbackDescriptor)) {
1271+
addCandidateEntry(result, candidate, descriptor, requiredType);
12721272
}
12731273
}
1274-
if (result.isEmpty() && !(descriptor instanceof MultiElementDependencyDescriptor)) {
1274+
if (result.isEmpty()) {
12751275
// Consider self references as a final pass...
1276-
// but not as collection elements, just for direct dependency declarations.
1277-
for (String candidateName : candidateNames) {
1278-
if (isSelfReference(beanName, candidateName) && isAutowireCandidate(candidateName, fallbackDescriptor)) {
1279-
addCandidateEntry(result, candidateName, descriptor, requiredType);
1276+
// but in the case of a dependency collection, not the very same bean itself.
1277+
for (String candidate : candidateNames) {
1278+
if (isSelfReference(beanName, candidate) &&
1279+
(!(descriptor instanceof MultiElementDescriptor) || !beanName.equals(candidate)) &&
1280+
isAutowireCandidate(candidate, fallbackDescriptor)) {
1281+
addCandidateEntry(result, candidate, descriptor, requiredType);
12801282
}
12811283
}
12821284
}
@@ -1291,7 +1293,7 @@ protected Map<String, Object> findAutowireCandidates(
12911293
private void addCandidateEntry(Map<String, Object> candidates, String candidateName,
12921294
DependencyDescriptor descriptor, Class<?> requiredType) {
12931295

1294-
if (descriptor instanceof MultiElementDependencyDescriptor || containsSingleton(candidateName)) {
1296+
if (descriptor instanceof MultiElementDescriptor || containsSingleton(candidateName)) {
12951297
candidates.put(candidateName, descriptor.resolveCandidate(candidateName, requiredType, this));
12961298
}
12971299
else {
@@ -1745,9 +1747,9 @@ public NestedDependencyDescriptor(DependencyDescriptor original) {
17451747
}
17461748

17471749

1748-
private static class MultiElementDependencyDescriptor extends NestedDependencyDescriptor {
1750+
private static class MultiElementDescriptor extends NestedDependencyDescriptor {
17491751

1750-
public MultiElementDependencyDescriptor(DependencyDescriptor original) {
1752+
public MultiElementDescriptor(DependencyDescriptor original) {
17511753
super(original);
17521754
}
17531755
}

0 commit comments

Comments
 (0)