@@ -399,6 +399,7 @@ a dependency on a specific bean through metadata (e.g. an autowiring annotation)
399
399
400
400
[[beans-definition]]
401
401
== Bean overview
402
+
402
403
A Spring IoC container manages one or more __beans__. These beans are created with the
403
404
configuration metadata that you supply to the container, for example, in the form of XML
404
405
`<bean/>` definitions.
@@ -777,6 +778,7 @@ Spring container that will create objects through an
777
778
778
779
[[beans-dependencies]]
779
780
== Dependencies
781
+
780
782
A typical enterprise application does not consist of a single object (or bean in the
781
783
Spring parlance). Even the simplest application has a few objects that work together to
782
784
present what the end-user sees as a coherent application. This next section explains how
@@ -2378,6 +2380,7 @@ shortest string that will match an argument type.
2378
2380
2379
2381
[[beans-factory-scopes]]
2380
2382
== Bean scopes
2383
+
2381
2384
When you create a bean definition, you create a __recipe__ for creating actual instances
2382
2385
of the class defined by that bean definition. The idea that a bean definition is a
2383
2386
recipe is important, because it means that, as with a class, you can create many object
@@ -3635,6 +3638,7 @@ beans that require programmatic access to the container.
3635
3638
3636
3639
[[beans-child-bean-definitions]]
3637
3640
== Bean definition inheritance
3641
+
3638
3642
A bean definition can contain a lot of configuration information, including constructor
3639
3643
arguments, property values, and container-specific information such as initialization
3640
3644
method, static factory method name, and so on. A child bean definition inherits
@@ -3720,6 +3724,7 @@ context will actually (attempt to) pre-instantiate the `abstract` bean.
3720
3724
3721
3725
[[beans-factory-extension]]
3722
3726
== Container Extension Points
3727
+
3723
3728
Typically, an application developer does not need to subclass `ApplicationContext`
3724
3729
implementation classes. Instead, the Spring IoC container can be extended by plugging in
3725
3730
implementations of special integration interfaces. The next few sections describe these
@@ -4237,11 +4242,13 @@ applicability. Spring 2.5 also added support for JSR-250 annotations such as
4237
4242
Injection for Java) annotations contained in the javax.inject package such as `@Inject`
4238
4243
and `@Named`. Details about those annotations can be found in the
4239
4244
<<beans-standard-annotations,relevant section>>.
4245
+
4240
4246
[NOTE]
4241
4247
====
4242
4248
Annotation injection is performed __before__ XML injection, thus the latter
4243
4249
configuration will override the former for properties wired through both approaches.
4244
4250
====
4251
+
4245
4252
As always, you can register them as individual bean definitions, but they can also be
4246
4253
implicitly registered by including the following tag in an XML-based Spring
4247
4254
configuration (notice the inclusion of the `context` namespace):
@@ -4513,13 +4520,43 @@ non-required constructors can be annotated. In that case, each is considered amo
4513
4520
candidates and Spring uses the __greediest__ constructor whose dependencies can be
4514
4521
satisfied, that is the constructor that has the largest number of arguments.
4515
4522
4516
- `@Autowired`'s __required__ attribute is recommended over the `@Required` annotation.
4523
+ The __required__ attribute of `@Autowired` is recommended over the `@Required` annotation.
4517
4524
The __required__ attribute indicates that the property is not required for autowiring
4518
4525
purposes, the property is ignored if it cannot be autowired. `@Required`, on the other
4519
4526
hand, is stronger in that it enforces the property that was set by any means supported
4520
4527
by the container. If no value is injected, a corresponding exception is raised.
4521
4528
====
4522
4529
4530
+ Alternatively, you may express the non-required nature of a particular dependency
4531
+ through Java 8's `java.util.Optional`:
4532
+
4533
+ [source,java,indent=0]
4534
+ [subs="verbatim,quotes"]
4535
+ ----
4536
+ public class SimpleMovieLister {
4537
+
4538
+ @Autowired
4539
+ public void setMovieFinder(Optional<MovieFinder> movieFinder) {
4540
+ ...
4541
+ }
4542
+ }
4543
+ ----
4544
+
4545
+ As of Spring Framework 5.0, you may also use an `@Nullable` annotation (of any kind
4546
+ in any package, e.g. `javax.annotation.Nullable` from JSR-305):
4547
+
4548
+ [source,java,indent=0]
4549
+ [subs="verbatim,quotes"]
4550
+ ----
4551
+ public class SimpleMovieLister {
4552
+
4553
+ @Autowired
4554
+ public void setMovieFinder(@Nullable MovieFinder movieFinder) {
4555
+ ...
4556
+ }
4557
+ }
4558
+ ----
4559
+
4523
4560
You can also use `@Autowired` for interfaces that are well-known resolvable
4524
4561
dependencies: `BeanFactory`, `ApplicationContext`, `Environment`, `ResourceLoader`,
4525
4562
`ApplicationEventPublisher`, and `MessageSource`. These interfaces and their extended
@@ -4550,6 +4587,7 @@ any). These types must be 'wired up' explicitly via XML or using a Spring `@Bean
4550
4587
====
4551
4588
4552
4589
4590
+
4553
4591
[[beans-autowired-annotation-primary]]
4554
4592
=== Fine-tuning annotation-based autowiring with @Primary
4555
4593
@@ -4726,9 +4764,16 @@ be injected into a `Set<MovieCatalog>` annotated with `@Qualifier("action")`.
4726
4764
4727
4765
[TIP]
4728
4766
====
4729
- If you intend to express annotation-driven injection by name, do not primarily use
4730
- `@Autowired`, even if is technically capable of referring to a bean name through
4731
- `@Qualifier` values. Instead, use the JSR-250 `@Resource` annotation, which is
4767
+ Letting qualifier values select against target bean names, within the type-matching
4768
+ candidates, doesn't even require a `@Qualifier` annotation at the injection point.
4769
+ If there is no other resolution indicator (e.g. a qualifier or a primary marker),
4770
+ for a non-unique dependency situation, Spring will match the injection point name
4771
+ (i.e. field name or parameter name) against the target bean names and choose the
4772
+ same-named candidate, if any.
4773
+
4774
+ That said, if you intend to express annotation-driven injection by name, do not
4775
+ primarily use `@Autowired`, even if is capable of selecting by bean name among
4776
+ type-matching candidates. Instead, use the JSR-250 `@Resource` annotation, which is
4732
4777
semantically defined to identify a specific target component by its unique name, with
4733
4778
the declared type being irrelevant for the matching process. `@Autowired` has rather
4734
4779
different semantics: After selecting candidate beans by type, the specified String
@@ -4896,7 +4941,6 @@ consider the following annotation definition:
4896
4941
String genre();
4897
4942
4898
4943
Format format();
4899
-
4900
4944
}
4901
4945
----
4902
4946
@@ -5378,7 +5422,7 @@ comma/semicolon/space-separated list that includes the parent package of each cl
5378
5422
5379
5423
[NOTE]
5380
5424
====
5381
- for concision, the above may have used the `value` attribute of the
5425
+ For concision, the above may have used the `value` attribute of the
5382
5426
annotation, i.e. `@ComponentScan("org.example")`
5383
5427
====
5384
5428
@@ -5427,7 +5471,7 @@ wired together - all without any bean configuration metadata provided in XML.
5427
5471
====
5428
5472
You can disable the registration of `AutowiredAnnotationBeanPostProcessor` and
5429
5473
`CommonAnnotationBeanPostProcessor` by including the __annotation-config__ attribute
5430
- with a value of false.
5474
+ with a value of ` false` .
5431
5475
====
5432
5476
5433
5477
@@ -5865,6 +5909,7 @@ metadata is provided per-instance rather than per-class.
5865
5909
5866
5910
[[beans-standard-annotations]]
5867
5911
== Using JSR 330 Standard Annotations
5912
+
5868
5913
Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations
5869
5914
(Dependency Injection). Those annotations are scanned in the same way as the Spring
5870
5915
annotations. You just need to have the relevant jars in your classpath.
@@ -5964,6 +6009,34 @@ you should use the `@Named` annotation as follows:
5964
6009
}
5965
6010
----
5966
6011
6012
+ Like `@Autowired`, `@Inject` can also be used with `java.util.Optional` or
6013
+ `@Nullable`. This is even more applicable here since `@Inject` does not have
6014
+ a `required` attribute.
6015
+
6016
+ [source,java,indent=0]
6017
+ [subs="verbatim,quotes"]
6018
+ ----
6019
+ public class SimpleMovieLister {
6020
+
6021
+ @Inject
6022
+ public void setMovieFinder(Optional<MovieFinder> movieFinder) {
6023
+ ...
6024
+ }
6025
+ }
6026
+ ----
6027
+
6028
+ [source,java,indent=0]
6029
+ [subs="verbatim,quotes"]
6030
+ ----
6031
+ public class SimpleMovieLister {
6032
+
6033
+ @Inject
6034
+ public void setMovieFinder(@Nullable MovieFinder movieFinder) {
6035
+ ...
6036
+ }
6037
+ }
6038
+ ----
6039
+
5967
6040
5968
6041
5969
6042
[[beans-named]]
@@ -6712,6 +6785,7 @@ annotation can be used:
6712
6785
----
6713
6786
6714
6787
6788
+
6715
6789
[[beans-java-configuration-annotation]]
6716
6790
=== Using the @Configuration annotation
6717
6791
@@ -7949,7 +8023,7 @@ AspectJ load-time weaving, see <<aop-aj-ltw>>.
7949
8023
7950
8024
7951
8025
[[context-introduction]]
7952
- == Additional Capabilities of the ApplicationContext
8026
+ == Additional capabilities of the ApplicationContext
7953
8027
7954
8028
As was discussed in the chapter introduction, the `org.springframework.beans.factory`
7955
8029
package provides basic functionality for managing and manipulating beans, including in a
@@ -8181,7 +8255,7 @@ out the `ReloadableResourceBundleMessageSource` javadocs for details.
8181
8255
8182
8256
8183
8257
[[context-functionality-events]]
8184
- === Standard and Custom Events
8258
+ === Standard and custom events
8185
8259
8186
8260
Event handling in the `ApplicationContext` is provided through the `ApplicationEvent`
8187
8261
class and `ApplicationListener` interface. If a bean that implements the
@@ -8370,8 +8444,9 @@ http://www.enterpriseintegrationpatterns.com[pattern-oriented], event-driven
8370
8444
architectures that build upon the well-known Spring programming model.
8371
8445
====
8372
8446
8447
+
8373
8448
[[context-functionality-events-annotation]]
8374
- ==== Annotation-based Event Listeners
8449
+ ==== Annotation-based event listeners
8375
8450
8376
8451
As of Spring 4.2, an event listener can be registered on any public method of a managed
8377
8452
bean via the `EventListener` annotation. The `BlackListNotifier` can be rewritten as
@@ -8478,6 +8553,7 @@ This new method will publish a new `ListUpdateEvent` for every `BlackListEvent`
8478
8553
by the method above. If you need to publish several events, just return a `Collection` of
8479
8554
events instead.
8480
8555
8556
+
8481
8557
[[context-functionality-events-async]]
8482
8558
==== Asynchronous Listeners
8483
8559
@@ -8504,7 +8580,7 @@ Be aware of the following limitations when using asynchronous events:
8504
8580
8505
8581
8506
8582
[[context-functionality-events-order]]
8507
- ==== Ordering Listeners
8583
+ ==== Ordering listeners
8508
8584
8509
8585
If you need the listener to be invoked before another one, just add the `@Order`
8510
8586
annotation to the method declaration:
@@ -8519,8 +8595,9 @@ annotation to the method declaration:
8519
8595
}
8520
8596
----
8521
8597
8598
+
8522
8599
[[context-functionality-events-generics]]
8523
- ==== Generic Events
8600
+ ==== Generic events
8524
8601
8525
8602
You may also use generics to further define the structure of your event. Consider an
8526
8603
`EntityCreatedEvent<T>` where `T` is the type of the actual entity that got created. You
@@ -8686,14 +8763,15 @@ be used by other application modules on the same machine.
8686
8763
8687
8764
[[beans-beanfactory]]
8688
8765
== The BeanFactory
8766
+
8689
8767
The `BeanFactory` provides the underlying basis for Spring's IoC functionality but it is
8690
8768
only used directly in integration with other third-party frameworks and is now largely
8691
8769
historical in nature for most users of Spring. The `BeanFactory` and related interfaces,
8692
8770
such as `BeanFactoryAware`, `InitializingBean`, `DisposableBean`, are still present in
8693
8771
Spring for the purposes of backward compatibility with the large number of third-party
8694
8772
frameworks that integrate with Spring. Often third-party components that can not use
8695
- more modern equivalents such as `@PostConstruct` or `@PreDestroy` in order to remain
8696
- compatible with JDK 1.4 or to avoid a dependency on JSR-250.
8773
+ more modern equivalents such as `@PostConstruct` or `@PreDestroy` in order to avoid a
8774
+ dependency on JSR-250.
8697
8775
8698
8776
This section provides additional background into the differences between the
8699
8777
`BeanFactory` and `ApplicationContext` and how one might access the IoC container
0 commit comments