Skip to content

Commit bb1699a

Browse files
committed
Revise documentation for autowiring with annotated constructors
Closes gh-24711
1 parent 10d47d4 commit bb1699a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5085,21 +5085,22 @@ without an `@Autowired` annotation.
50855085
====
50865086
Only one constructor of any given bean class may declare `@Autowired` with the `required`
50875087
attribute set to `true`, indicating _the_ constructor to autowire when used as a Spring
5088-
bean. Furthermore, if the `required` attribute is set to `true`, only a single
5089-
constructor may be annotated with `@Autowired`. If multiple _non-required_ constructors
5090-
declare the annotation, they will be considered as candidates for autowiring. The
5091-
constructor with the greatest number of dependencies that can be satisfied by matching
5088+
bean. As a consequence, if the `required` attribute is left at its default value `true`,
5089+
only a single constructor may be annotated with `@Autowired`. If multiple constructors
5090+
declare the annotation, they will all have to declare `required=false` in order to be
5091+
considered as candidates for autowiring (analogous to `autowire=constructor` in XML).
5092+
The constructor with the greatest number of dependencies that can be satisfied by matching
50925093
beans in the Spring container will be chosen. If none of the candidates can be satisfied,
50935094
then a primary/default constructor (if present) will be used. If a class only declares a
5094-
single constructor to begin with, it will always be used, even if not annotated. An
5095-
annotated constructor does not have to be public.
5095+
single constructor to begin with, it will always be used, even if not annotated. Note that
5096+
an annotated constructor does not have to be public.
50965097
50975098
The `required` attribute of `@Autowired` is recommended over the deprecated `@Required`
50985099
annotation on setter methods. Setting the `required` attribute to `false` indicates that
50995100
the property is not required for autowiring purposes, and the property is ignored if it
51005101
cannot be autowired. `@Required`, on the other hand, is stronger in that it enforces the
5101-
property to be set by any means supported by the container, and if no value is defined, a
5102-
corresponding exception is raised.
5102+
property to be set by any means supported by the container, and if no value is defined,
5103+
a corresponding exception is raised.
51035104
====
51045105

51055106
Alternatively, you can express the non-required nature of a particular dependency

0 commit comments

Comments
 (0)