Spring's constructor resolution consistently finds non-public multi-arg constructors (SPR-7453)

This commit is contained in:
Juergen Hoeller 2010-08-11 19:24:30 +00:00
parent 6e303d25c4
commit 8a23ce917a
3 changed files with 10 additions and 12 deletions

View File

@ -43,8 +43,8 @@ import org.springframework.util.ClassUtils;
abstract class AutowireUtils { abstract class AutowireUtils {
/** /**
* Sort the given constructors, preferring public constructors and "greedy" ones * Sort the given constructors, preferring public constructors and "greedy" ones with
* with a maximum of arguments. The result will contain public constructors first, * a maximum number of arguments. The result will contain public constructors first,
* with decreasing number of arguments, then non-public constructors, again with * with decreasing number of arguments, then non-public constructors, again with
* decreasing number of arguments. * decreasing number of arguments.
* @param constructors the constructor array to sort * @param constructors the constructor array to sort

View File

@ -172,10 +172,7 @@ class ConstructorResolver {
break; break;
} }
if (paramTypes.length < minNrOfArgs) { if (paramTypes.length < minNrOfArgs) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, continue;
minNrOfArgs + " constructor arguments specified but no matching constructor found in bean '" +
beanName + "' " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
} }
ArgumentsHolder argsHolder; ArgumentsHolder argsHolder;
@ -245,8 +242,9 @@ class ConstructorResolver {
} }
if (constructorToUse == null) { if (constructorToUse == null) {
throw new BeanCreationException( throw new BeanCreationException(mbd.getResourceDescription(), beanName,
mbd.getResourceDescription(), beanName, "Could not resolve matching constructor"); "Could not resolve matching constructor " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
} }
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) { else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName,
@ -663,16 +661,16 @@ class ConstructorResolver {
// We found a potential match - let's give it a try. // We found a potential match - let's give it a try.
// Do not consider the same value definition multiple times! // Do not consider the same value definition multiple times!
usedValueHolders.add(valueHolder); usedValueHolders.add(valueHolder);
ConstructorArgumentValues.ValueHolder sourceHolder =
(ConstructorArgumentValues.ValueHolder) valueHolder.getSource();
Object originalValue = valueHolder.getValue(); Object originalValue = valueHolder.getValue();
Object sourceValue = sourceHolder.getValue();
Object convertedValue; Object convertedValue;
if (valueHolder.isConverted()) { if (valueHolder.isConverted()) {
convertedValue = valueHolder.getConvertedValue(); convertedValue = valueHolder.getConvertedValue();
args.preparedArguments[paramIndex] = convertedValue; args.preparedArguments[paramIndex] = convertedValue;
} }
else { else {
ConstructorArgumentValues.ValueHolder sourceHolder =
(ConstructorArgumentValues.ValueHolder) valueHolder.getSource();
Object sourceValue = sourceHolder.getValue();
try { try {
convertedValue = converter.convertIfNecessary(originalValue, paramType, convertedValue = converter.convertIfNecessary(originalValue, paramType,
MethodParameter.forMethodOrConstructor(methodOrCtor, paramIndex)); MethodParameter.forMethodOrConstructor(methodOrCtor, paramIndex));

View File

@ -738,7 +738,7 @@ class SingleSimpleTypeConstructorBean {
this.singleBoolean = singleBoolean; this.singleBoolean = singleBoolean;
} }
public SingleSimpleTypeConstructorBean(String testString, boolean secondBoolean) { protected SingleSimpleTypeConstructorBean(String testString, boolean secondBoolean) {
this.testString = testString; this.testString = testString;
this.secondBoolean = secondBoolean; this.secondBoolean = secondBoolean;
} }