Skip to content

Commit 56eadff

Browse files
committed
Merge branch '5.1.x'
2 parents e92cbe1 + 74ddf1b commit 56eadff

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/Autowired.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@
2828
* {@link javax.inject.Inject} annotation, adding required-vs-optional semantics.
2929
*
3030
* <h3>Autowired Constructors</h3>
31-
* <p>Only one constructor (at max) of any given bean class may declare this annotation
32-
* with the 'required' parameter set to {@code true}, indicating <i>the</i> constructor
33-
* to autowire when used as a Spring bean. If multiple <i>non-required</i> constructors
34-
* declare the annotation, they will be considered as candidates for autowiring.
35-
* The constructor with the greatest number of dependencies that can be satisfied by
36-
* matching beans in the Spring container will be chosen. If none of the candidates
37-
* can be satisfied, then a primary/default constructor (if present) will be used.
38-
* If a class only declares a single constructor to begin with, it will always be used,
39-
* even if not annotated. An annotated constructor does not have to be public.
31+
* <p>Only one constructor of any given bean class may declare this annotation with
32+
* the 'required' attribute set to {@code true}, indicating <i>the</i> constructor
33+
* to autowire when used as a Spring bean. Furthermore, if the 'required' attribute
34+
* is set to {@code true}, only a single constructor may be annotated with
35+
* {@code @Autowired}. If multiple <i>non-required</i> constructors declare the
36+
* annotation, they will be considered as candidates for autowiring. The constructor
37+
* with the greatest number of dependencies that can be satisfied by matching beans
38+
* in the Spring container will be chosen. If none of the candidates can be satisfied,
39+
* then a primary/default constructor (if present) will be used. If a class only
40+
* declares a single constructor to begin with, it will always be used, even if not
41+
* annotated. An annotated constructor does not have to be public.
4042
*
4143
* <h3>Autowired Fields</h3>
4244
* <p>Fields are injected right after construction of a bean, before any config methods

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,15 +4538,19 @@ e.g. declared as a single public constructor without an `@Autowired` annotation.
45384538

45394539
[NOTE]
45404540
====
4541-
Only one annotated constructor per class can be marked as required, but multiple
4542-
non-required constructors can be annotated. In that case, each is considered among
4543-
the candidates and Spring uses the greediest constructor whose dependencies can be
4544-
satisfied -- that is, the constructor that has the largest number of arguments.
4545-
The constructor resolution algorithm is the same as for non-annotated classes with
4546-
overloaded constructors, just narrowing the candidates to annotated constructors.
4547-
4548-
The 'required' attribute of `@Autowired` is recommended over the `@Required` annotation
4549-
on setter methods. The 'required' attribute indicates that the property is not required
4541+
Only one constructor of any given bean class may declare `@Autowired` with the `required`
4542+
attribute set to `true`, indicating _the_ constructor to autowire when used as a Spring
4543+
bean. Furthermore, if the `required` attribute is set to `true`, only a single
4544+
constructor may be annotated with `@Autowired`. If multiple _non-required_ constructors
4545+
declare the annotation, they will be considered as candidates for autowiring. The
4546+
constructor with the greatest number of dependencies that can be satisfied by matching
4547+
beans in the Spring container will be chosen. If none of the candidates can be satisfied,
4548+
then a primary/default constructor (if present) will be used. If a class only declares a
4549+
single constructor to begin with, it will always be used, even if not annotated. An
4550+
annotated constructor does not have to be public.
4551+
4552+
The `required` attribute of `@Autowired` is recommended over the `@Required` annotation
4553+
on setter methods. The `required` attribute indicates that the property is not required
45504554
for autowiring purposes. The property is ignored if it cannot be autowired. `@Required`,
45514555
on the other hand, is stronger in that it enforces the property to be set by any means
45524556
supported by the container. If no value is defined, a corresponding exception is raised.

0 commit comments

Comments
 (0)