Automatically autowire a bean with one constructor

Previously, if a managed bean had only one non-default constructor, we
should still annotate it with `@Autowired` to properly use constructor
injection. Not doing so resulted in an error as the container was
trying to call the default (non-existing) constructor.

This commit updates this behaviour to automatically applyed the
autowiring semantic to any bean that has only one constructor. As
before, if more than one constructor is defined, `@Autowired` must be
specified to teach the container the constructor it has to use.

Issue: SPR-12278
This commit is contained in:
Stephane Nicoll
2015-11-26 16:05:53 +01:00
parent 3d87718fc6
commit 9e7c791a0f
3 changed files with 128 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -107,6 +107,7 @@ import org.springframework.util.StringUtils;
*
* @author Juergen Hoeller
* @author Mark Fisher
* @author Stephane Nicoll
* @since 2.5
* @see #setAutowiredAnnotationType
* @see Autowired
@@ -312,6 +313,9 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
}
candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
}
else if (rawCandidates.length == 1 && rawCandidates[0].getParameterTypes().length > 0) {
candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
}
else {
candidateConstructors = new Constructor<?>[0];
}