Skip to content

Commit 8a23ce9

Browse files
committed
Spring's constructor resolution consistently finds non-public multi-arg constructors (SPR-7453)
1 parent 6e303d2 commit 8a23ce9

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
abstract class AutowireUtils {
4444

4545
/**
46-
* Sort the given constructors, preferring public constructors and "greedy" ones
47-
* with a maximum of arguments. The result will contain public constructors first,
46+
* Sort the given constructors, preferring public constructors and "greedy" ones with
47+
* a maximum number of arguments. The result will contain public constructors first,
4848
* with decreasing number of arguments, then non-public constructors, again with
4949
* decreasing number of arguments.
5050
* @param constructors the constructor array to sort

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ public BeanWrapper autowireConstructor(
172172
break;
173173
}
174174
if (paramTypes.length < minNrOfArgs) {
175-
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
176-
minNrOfArgs + " constructor arguments specified but no matching constructor found in bean '" +
177-
beanName + "' " +
178-
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
175+
continue;
179176
}
180177

181178
ArgumentsHolder argsHolder;
@@ -245,8 +242,9 @@ else if (constructorToUse != null && typeDiffWeight == minTypeDiffWeight) {
245242
}
246243

247244
if (constructorToUse == null) {
248-
throw new BeanCreationException(
249-
mbd.getResourceDescription(), beanName, "Could not resolve matching constructor");
245+
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
246+
"Could not resolve matching constructor " +
247+
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
250248
}
251249
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
252250
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
@@ -663,16 +661,16 @@ private ArgumentsHolder createArgumentArray(
663661
// We found a potential match - let's give it a try.
664662
// Do not consider the same value definition multiple times!
665663
usedValueHolders.add(valueHolder);
666-
ConstructorArgumentValues.ValueHolder sourceHolder =
667-
(ConstructorArgumentValues.ValueHolder) valueHolder.getSource();
668664
Object originalValue = valueHolder.getValue();
669-
Object sourceValue = sourceHolder.getValue();
670665
Object convertedValue;
671666
if (valueHolder.isConverted()) {
672667
convertedValue = valueHolder.getConvertedValue();
673668
args.preparedArguments[paramIndex] = convertedValue;
674669
}
675670
else {
671+
ConstructorArgumentValues.ValueHolder sourceHolder =
672+
(ConstructorArgumentValues.ValueHolder) valueHolder.getSource();
673+
Object sourceValue = sourceHolder.getValue();
676674
try {
677675
convertedValue = converter.convertIfNecessary(originalValue, paramType,
678676
MethodParameter.forMethodOrConstructor(methodOrCtor, paramIndex));

org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTestTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ public SingleSimpleTypeConstructorBean(boolean singleBoolean) {
738738
this.singleBoolean = singleBoolean;
739739
}
740740

741-
public SingleSimpleTypeConstructorBean(String testString, boolean secondBoolean) {
741+
protected SingleSimpleTypeConstructorBean(String testString, boolean secondBoolean) {
742742
this.testString = testString;
743743
this.secondBoolean = secondBoolean;
744744
}

0 commit comments

Comments
 (0)