Skip to content

Commit 97b83a3

Browse files
committed
Revised documentation on constructor autowiring semantics
Closes gh-22735
1 parent 325fb5d commit 97b83a3

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4690,9 +4690,9 @@ implementation type, consider declaring the most specific return type on your fa
46904690
method (at least as specific as required by the injection points referring to your bean).
46914691
====
46924692

4693-
You can also provide all beans of a particular type from the
4694-
`ApplicationContext` by adding the annotation to a field or method that expects an array
4695-
of that type, as the following example shows:
4693+
You can also provide all beans of a particular type from the `ApplicationContext`
4694+
by adding the annotation to a field or method that expects an array of that type,
4695+
as the following example shows:
46964696

46974697
====
46984698
[source,java,indent=0]
@@ -4746,8 +4746,8 @@ Note that the standard `javax.annotation.Priority` annotation is not available a
47464746
through `@Order` values in combination with `@Primary` on a single bean for each type.
47474747
====
47484748

4749-
Even typed `Map` instances can be autowired as long as the expected key type is `String`. The Map
4750-
values contain all beans of the expected type, and the keys contain the
4749+
Even typed `Map` instances can be autowired as long as the expected key type is `String`.
4750+
The Map values contain all beans of the expected type, and the keys contain the
47514751
corresponding bean names, as the following example shows:
47524752

47534753
====
@@ -4768,10 +4768,14 @@ corresponding bean names, as the following example shows:
47684768
----
47694769
====
47704770

4771-
By default, the autowiring fails whenever zero candidate beans are available. The
4772-
default behavior is to treat annotated methods, constructors, and fields as
4773-
indicating required dependencies. You can change this behavior as demonstrated, in the
4774-
following example:
4771+
By default, autowiring fails when no matching candidate beans are available for
4772+
a given injection point. In the case of a declared array, collection or map,
4773+
at least one matching element is expected.
4774+
4775+
The default behavior is to treat annotated methods and fields as indicating
4776+
required dependencies. You can change this behavior as demonstrated in the
4777+
following example, enabling the framework to skip a non-satisfiable injection
4778+
point through marking it as non-required:
47754779

47764780
====
47774781
[source,java,indent=0]
@@ -4791,18 +4795,34 @@ following example:
47914795
----
47924796
====
47934797

4798+
A non-required method will not be called at all if its dependency (or one of its
4799+
dependencies in case of multiple arguments) is not available. A non-required field
4800+
will not get populated at all in such case, leaving its default value in place.
4801+
4802+
Injected constructor and factory method arguments are a special case since the
4803+
'required' flag on `@Autowired` has a somewhat different meaning due to Spring's
4804+
constructor resolution algorithm potentially dealing with multiple constructors.
4805+
Constructor and factory method arguments are effectively required by default but
4806+
with a few special rules in a single-constructor scenario, such as multi-element
4807+
injection points (arrays, collections, maps) resolving to empty instances if no
4808+
matching beans are available. This allows for a common implementation pattern
4809+
where all dependencies can be declared in a unique multi-argument constructor,
4810+
e.g. declared as a single public constructor without an `@Autowired` annotation.
4811+
47944812
[NOTE]
47954813
====
4796-
Only one annotated constructor per-class can be marked as required, but multiple
4797-
non-required constructors can be annotated. In that case, each is considered among the
4798-
candidates and Spring uses the greediest constructor whose dependencies can be
4814+
Only one annotated constructor per class can be marked as required, but multiple
4815+
non-required constructors can be annotated. In that case, each is considered among
4816+
the candidates and Spring uses the greediest constructor whose dependencies can be
47994817
satisfied -- that is, the constructor that has the largest number of arguments.
4800-
4801-
The required attribute of `@Autowired` is recommended over the `@Required` annotation.
4802-
The required attribute indicates that the property is not required for autowiring
4803-
purposes. The property is ignored if it cannot be autowired. `@Required`, on the other
4804-
hand, is stronger in that it enforces the property that was set by any means supported
4805-
by the container. If no value is injected, a corresponding exception is raised.
4818+
The constructor resolution algorithm is the same as for non-annotated classes with
4819+
overloaded constructors, just narrowing the candidates to annotated constructors.
4820+
4821+
The 'required' attribute of `@Autowired` is recommended over the `@Required` annotation
4822+
on setter methods. The 'required' attribute indicates that the property is not required
4823+
for autowiring purposes. The property is ignored if it cannot be autowired. `@Required`,
4824+
on the other hand, is stronger in that it enforces the property to be set by any means
4825+
supported by the container. If no value is defined, a corresponding exception is raised.
48064826
====
48074827

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

0 commit comments

Comments
 (0)