@@ -4690,9 +4690,9 @@ implementation type, consider declaring the most specific return type on your fa
4690
4690
method (at least as specific as required by the injection points referring to your bean).
4691
4691
====
4692
4692
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:
4696
4696
4697
4697
====
4698
4698
[source,java,indent=0]
@@ -4746,8 +4746,8 @@ Note that the standard `javax.annotation.Priority` annotation is not available a
4746
4746
through `@Order` values in combination with `@Primary` on a single bean for each type.
4747
4747
====
4748
4748
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
4751
4751
corresponding bean names, as the following example shows:
4752
4752
4753
4753
====
@@ -4768,10 +4768,14 @@ corresponding bean names, as the following example shows:
4768
4768
----
4769
4769
====
4770
4770
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:
4775
4779
4776
4780
====
4777
4781
[source,java,indent=0]
@@ -4791,18 +4795,34 @@ following example:
4791
4795
----
4792
4796
====
4793
4797
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
+
4794
4812
[NOTE]
4795
4813
====
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
4799
4817
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.
4806
4826
====
4807
4827
4808
4828
Alternatively, you can express the non-required nature of a particular dependency
0 commit comments