@@ -4425,9 +4425,9 @@ implementation type, consider declaring the most specific return type on your fa
4425
4425
method (at least as specific as required by the injection points referring to your bean).
4426
4426
====
4427
4427
4428
- You can also provide all beans of a particular type from the
4429
- `ApplicationContext` by adding the annotation to a field or method that expects an array
4430
- of that type, as the following example shows:
4428
+ You can also provide all beans of a particular type from the `ApplicationContext`
4429
+ by adding the annotation to a field or method that expects an array of that type,
4430
+ as the following example shows:
4431
4431
4432
4432
[source,java,indent=0]
4433
4433
[subs="verbatim,quotes"]
@@ -4477,8 +4477,8 @@ Note that the standard `javax.annotation.Priority` annotation is not available a
4477
4477
through `@Order` values in combination with `@Primary` on a single bean for each type.
4478
4478
====
4479
4479
4480
- Even typed `Map` instances can be autowired as long as the expected key type is `String`. The Map
4481
- values contain all beans of the expected type, and the keys contain the
4480
+ Even typed `Map` instances can be autowired as long as the expected key type is `String`.
4481
+ The Map values contain all beans of the expected type, and the keys contain the
4482
4482
corresponding bean names, as the following example shows:
4483
4483
4484
4484
[source,java,indent=0]
@@ -4497,10 +4497,14 @@ corresponding bean names, as the following example shows:
4497
4497
}
4498
4498
----
4499
4499
4500
- By default, the autowiring fails whenever zero candidate beans are available. The
4501
- default behavior is to treat annotated methods, constructors, and fields as
4502
- indicating required dependencies. You can change this behavior as demonstrated, in the
4503
- following example:
4500
+ By default, autowiring fails when no matching candidate beans are available for
4501
+ a given injection point. In the case of a declared array, collection or map,
4502
+ at least one matching element is expected.
4503
+
4504
+ The default behavior is to treat annotated methods and fields as indicating
4505
+ required dependencies. You can change this behavior as demonstrated in the
4506
+ following example, enabling the framework to skip a non-satisfiable injection
4507
+ point through marking it as non-required:
4504
4508
4505
4509
[source,java,indent=0]
4506
4510
[subs="verbatim,quotes"]
@@ -4518,18 +4522,34 @@ following example:
4518
4522
}
4519
4523
----
4520
4524
4525
+ A non-required method will not be called at all if its dependency (or one of its
4526
+ dependencies in case of multiple arguments) is not available. A non-required field
4527
+ will not get populated at all in such case, leaving its default value in place.
4528
+
4529
+ Injected constructor and factory method arguments are a special case since the
4530
+ 'required' flag on `@Autowired` has a somewhat different meaning due to Spring's
4531
+ constructor resolution algorithm potentially dealing with multiple constructors.
4532
+ Constructor and factory method arguments are effectively required by default but
4533
+ with a few special rules in a single-constructor scenario, such as multi-element
4534
+ injection points (arrays, collections, maps) resolving to empty instances if no
4535
+ matching beans are available. This allows for a common implementation pattern
4536
+ where all dependencies can be declared in a unique multi-argument constructor,
4537
+ e.g. declared as a single public constructor without an `@Autowired` annotation.
4538
+
4521
4539
[NOTE]
4522
4540
====
4523
- Only one annotated constructor per- class can be marked as required, but multiple
4524
- non-required constructors can be annotated. In that case, each is considered among the
4525
- candidates and Spring uses the greediest constructor whose dependencies can be
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
4526
4544
satisfied -- that is, the constructor that has the largest number of arguments.
4527
-
4528
- The required attribute of `@Autowired` is recommended over the `@Required` annotation.
4529
- The required attribute indicates that the property is not required for autowiring
4530
- purposes. The property is ignored if it cannot be autowired. `@Required`, on the other
4531
- hand, is stronger in that it enforces the property that was set by any means supported
4532
- by the container. If no value is injected, a corresponding exception is raised.
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
4550
+ for autowiring purposes. The property is ignored if it cannot be autowired. `@Required`,
4551
+ on the other hand, is stronger in that it enforces the property to be set by any means
4552
+ supported by the container. If no value is defined, a corresponding exception is raised.
4533
4553
====
4534
4554
4535
4555
Alternatively, you can express the non-required nature of a particular dependency
0 commit comments