Log warning for single optional constructor when no default constructor to fall back to

Issue: SPR-12161
This commit is contained in:
Juergen Hoeller
2014-09-17 13:02:11 +02:00
parent 15320db414
commit 80cec011b7
2 changed files with 43 additions and 4 deletions

View File

@@ -299,8 +299,16 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
}
if (!candidates.isEmpty()) {
// Add default constructor to list of optional constructors, as fallback.
if (requiredConstructor == null && defaultConstructor != null) {
candidates.add(defaultConstructor);
if (requiredConstructor == null) {
if (defaultConstructor != null) {
candidates.add(defaultConstructor);
}
else if (candidates.size() == 1 && logger.isWarnEnabled()) {
logger.warn("Inconsistent constructor declaration on bean with name '" + beanName +
"': single autowire-marked constructor flagged as optional - this constructor " +
"is effectively required since there is no default constructor to fall back to: " +
candidates.get(0));
}
}
candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
}