@@ -4211,11 +4211,13 @@ applicability. Spring 2.5 also added support for JSR-250 annotations such as
4211
4211
Injection for Java) annotations contained in the javax.inject package such as `@Inject`
4212
4212
and `@Named`. Details about those annotations can be found in the
4213
4213
<<beans-standard-annotations,relevant section>>.
4214
+
4214
4215
[NOTE]
4215
4216
====
4216
4217
Annotation injection is performed __before__ XML injection, thus the latter
4217
4218
configuration will override the former for properties wired through both approaches.
4218
4219
====
4220
+
4219
4221
As always, you can register them as individual bean definitions, but they can also be
4220
4222
implicitly registered by including the following tag in an XML-based Spring
4221
4223
configuration (notice the inclusion of the `context` namespace):
@@ -4494,6 +4496,36 @@ hand, is stronger in that it enforces the property that was set by any means sup
4494
4496
by the container. If no value is injected, a corresponding exception is raised.
4495
4497
====
4496
4498
4499
+ Alternatively, you may express the non-required nature of a particular dependency
4500
+ through Java 8's `java.util.Optional`:
4501
+
4502
+ [source,java,indent=0]
4503
+ [subs="verbatim,quotes"]
4504
+ ----
4505
+ public class SimpleMovieLister {
4506
+
4507
+ @Autowired
4508
+ public void setMovieFinder(Optional<MovieFinder> movieFinder) {
4509
+ ...
4510
+ }
4511
+ }
4512
+ ----
4513
+
4514
+ As of Spring Framework 5.0, you may also use an `@Nullable` annotation (of any kind
4515
+ in any package, e.g. `javax.annotation.Nullable` from JSR-305):
4516
+
4517
+ [source,java,indent=0]
4518
+ [subs="verbatim,quotes"]
4519
+ ----
4520
+ public class SimpleMovieLister {
4521
+
4522
+ @Autowired
4523
+ public void setMovieFinder(@Nullable MovieFinder movieFinder) {
4524
+ ...
4525
+ }
4526
+ }
4527
+ ----
4528
+
4497
4529
You can also use `@Autowired` for interfaces that are well-known resolvable
4498
4530
dependencies: `BeanFactory`, `ApplicationContext`, `Environment`, `ResourceLoader`,
4499
4531
`ApplicationEventPublisher`, and `MessageSource`. These interfaces and their extended
@@ -4601,6 +4633,7 @@ The corresponding bean definitions appear as follows.
4601
4633
----
4602
4634
4603
4635
4636
+
4604
4637
[[beans-autowired-annotation-qualifiers]]
4605
4638
=== Fine-tuning annotation-based autowiring with qualifiers
4606
4639
@@ -4700,9 +4733,16 @@ be injected into a `Set<MovieCatalog>` annotated with `@Qualifier("action")`.
4700
4733
4701
4734
[TIP]
4702
4735
====
4703
- If you intend to express annotation-driven injection by name, do not primarily use
4704
- `@Autowired`, even if is technically capable of referring to a bean name through
4705
- `@Qualifier` values. Instead, use the JSR-250 `@Resource` annotation, which is
4736
+ Letting qualifier values select against target bean names, within the type-matching
4737
+ candidates, doesn't even require a `@Qualifier` annotation at the injection point.
4738
+ If there is no other resolution indicator (e.g. a qualifier or a primary marker),
4739
+ for a non-unique dependency situation, Spring will match the injection point name
4740
+ (i.e. field name or parameter name) against the target bean names and choose the
4741
+ same-named candidate, if any.
4742
+
4743
+ That said, if you intend to express annotation-driven injection by name, do not
4744
+ primarily use `@Autowired`, even if is capable of selecting by bean name among
4745
+ type-matching candidates. Instead, use the JSR-250 `@Resource` annotation, which is
4706
4746
semantically defined to identify a specific target component by its unique name, with
4707
4747
the declared type being irrelevant for the matching process. `@Autowired` has rather
4708
4748
different semantics: After selecting candidate beans by type, the specified String
@@ -4870,7 +4910,6 @@ consider the following annotation definition:
4870
4910
String genre();
4871
4911
4872
4912
Format format();
4873
-
4874
4913
}
4875
4914
----
4876
4915
@@ -6006,6 +6045,34 @@ you should use the `@Named` annotation as follows:
6006
6045
}
6007
6046
----
6008
6047
6048
+ Like `@Autowired`, `@Inject` can also be used with `java.util.Optional` or
6049
+ `@Nullable`. This is even more applicable here since `@Inject` does not have
6050
+ a `required` attribute.
6051
+
6052
+ [source,java,indent=0]
6053
+ [subs="verbatim,quotes"]
6054
+ ----
6055
+ public class SimpleMovieLister {
6056
+
6057
+ @Inject
6058
+ public void setMovieFinder(Optional<MovieFinder> movieFinder) {
6059
+ ...
6060
+ }
6061
+ }
6062
+ ----
6063
+
6064
+ [source,java,indent=0]
6065
+ [subs="verbatim,quotes"]
6066
+ ----
6067
+ public class SimpleMovieLister {
6068
+
6069
+ @Inject
6070
+ public void setMovieFinder(@Nullable MovieFinder movieFinder) {
6071
+ ...
6072
+ }
6073
+ }
6074
+ ----
6075
+
6009
6076
6010
6077
6011
6078
[[beans-named]]
0 commit comments