diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java index 36e91744666..242fddbad8e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java @@ -36,9 +36,11 @@ import java.lang.annotation.Target; * annotation, they will be considered as candidates for autowiring. The constructor * with the greatest number of dependencies that can be satisfied by matching beans * in the Spring container will be chosen. If none of the candidates can be satisfied, - * then a primary/default constructor (if present) will be used. If a class only - * declares a single constructor to begin with, it will always be used, even if not - * annotated. An annotated constructor does not have to be public. + * then a primary/default constructor (if present) will be used. Similarly, if a + * class declares multiple constructors but none of them is annotated with + * {@code @Autowired}, then a primary/default constructor (if present) will be used. + * If a class only declares a single constructor to begin with, it will always be used, + * even if not annotated. An annotated constructor does not have to be public. * *

Autowired Fields

*

Fields are injected right after construction of a bean, before any config methods diff --git a/src/docs/asciidoc/core/core-beans.adoc b/src/docs/asciidoc/core/core-beans.adoc index 7ab620ef548..2fbf14eddd6 100644 --- a/src/docs/asciidoc/core/core-beans.adoc +++ b/src/docs/asciidoc/core/core-beans.adoc @@ -4816,10 +4816,12 @@ You can apply the `@Autowired` annotation to constructors, as the following exam [NOTE] ==== -As of Spring Framework 4.3, an `@Autowired` annotation on such a constructor is -no longer necessary if the target bean defines only one constructor to begin with. -However, if several constructors are available, at least one must be annotated -with `@Autowired` in order to instruct the container which one to use. +As of Spring Framework 4.3, an `@Autowired` annotation on such a constructor is no longer +necessary if the target bean defines only one constructor to begin with. However, if +several constructors are available and there is no primary/default constructor, at least +one of the constructors must be annotated with `@Autowired` in order to instruct the +container which one to use. See the discussion on +<> for details. ==== You can also apply the `@Autowired` annotation to _traditional_ setter methods, @@ -5086,7 +5088,9 @@ non-required (i.e., by setting the `required` attribute in `@Autowired` to `fals A non-required method will not be called at all if its dependency (or one of its dependencies, in case of multiple arguments) is not available. A non-required field will -not get populated at all in such case, leaving its default value in place. +not get populated at all in such cases, leaving its default value in place. + +[[beans-autowired-annotation-constructor-resolution]] Injected constructor and factory method arguments are a special case since the `required` attribute in `@Autowired` has a somewhat different meaning due to Spring's constructor @@ -5108,9 +5112,11 @@ declare the annotation, they will all have to declare `required=false` in order considered as candidates for autowiring (analogous to `autowire=constructor` in XML). The constructor with the greatest number of dependencies that can be satisfied by matching beans in the Spring container will be chosen. If none of the candidates can be satisfied, -then a primary/default constructor (if present) will be used. If a class only declares a -single constructor to begin with, it will always be used, even if not annotated. Note that -an annotated constructor does not have to be public. +then a primary/default constructor (if present) will be used. Similarly, if a class +declares multiple constructors but none of them is annotated with `@Autowired`, then a +primary/default constructor (if present) will be used. If a class only declares a single +constructor to begin with, it will always be used, even if not annotated. Note that an +annotated constructor does not have to be public. The `required` attribute of `@Autowired` is recommended over the deprecated `@Required` annotation on setter methods. Setting the `required` attribute to `false` indicates that