@@ -399,6 +399,7 @@ a dependency on a specific bean through metadata (e.g. an autowiring annotation)
399399
400400[[beans-definition]]
401401== Bean overview
402+
402403A Spring IoC container manages one or more __beans__. These beans are created with the
403404configuration metadata that you supply to the container, for example, in the form of XML
404405`<bean/>` definitions.
@@ -777,6 +778,7 @@ Spring container that will create objects through an
777778
778779[[beans-dependencies]]
779780== Dependencies
781+
780782A typical enterprise application does not consist of a single object (or bean in the
781783Spring parlance). Even the simplest application has a few objects that work together to
782784present 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.
23782380
23792381[[beans-factory-scopes]]
23802382== Bean scopes
2383+
23812384When you create a bean definition, you create a __recipe__ for creating actual instances
23822385of the class defined by that bean definition. The idea that a bean definition is a
23832386recipe 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.
36353638
36363639[[beans-child-bean-definitions]]
36373640== Bean definition inheritance
3641+
36383642A bean definition can contain a lot of configuration information, including constructor
36393643arguments, property values, and container-specific information such as initialization
36403644method, 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.
37203724
37213725[[beans-factory-extension]]
37223726== Container Extension Points
3727+
37233728Typically, an application developer does not need to subclass `ApplicationContext`
37243729implementation classes. Instead, the Spring IoC container can be extended by plugging in
37253730implementations 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
42374242Injection for Java) annotations contained in the javax.inject package such as `@Inject`
42384243and `@Named`. Details about those annotations can be found in the
42394244<<beans-standard-annotations,relevant section>>.
4245+
42404246[NOTE]
42414247====
42424248Annotation injection is performed __before__ XML injection, thus the latter
42434249configuration will override the former for properties wired through both approaches.
42444250====
4251+
42454252As always, you can register them as individual bean definitions, but they can also be
42464253implicitly registered by including the following tag in an XML-based Spring
42474254configuration (notice the inclusion of the `context` namespace):
@@ -4513,13 +4520,43 @@ non-required constructors can be annotated. In that case, each is considered amo
45134520candidates and Spring uses the __greediest__ constructor whose dependencies can be
45144521satisfied, that is the constructor that has the largest number of arguments.
45154522
4516- `@Autowired`'s __required__ attribute is recommended over the `@Required` annotation.
4523+ The __required__ attribute of `@Autowired` is recommended over the `@Required` annotation.
45174524The __required__ attribute indicates that the property is not required for autowiring
45184525purposes, the property is ignored if it cannot be autowired. `@Required`, on the other
45194526hand, is stronger in that it enforces the property that was set by any means supported
45204527by the container. If no value is injected, a corresponding exception is raised.
45214528====
45224529
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+
45234560You can also use `@Autowired` for interfaces that are well-known resolvable
45244561dependencies: `BeanFactory`, `ApplicationContext`, `Environment`, `ResourceLoader`,
45254562`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
45504587====
45514588
45524589
4590+
45534591[[beans-autowired-annotation-primary]]
45544592=== Fine-tuning annotation-based autowiring with @Primary
45554593
@@ -4726,9 +4764,16 @@ be injected into a `Set<MovieCatalog>` annotated with `@Qualifier("action")`.
47264764
47274765[TIP]
47284766====
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
47324777semantically defined to identify a specific target component by its unique name, with
47334778the declared type being irrelevant for the matching process. `@Autowired` has rather
47344779different semantics: After selecting candidate beans by type, the specified String
@@ -4896,7 +4941,6 @@ consider the following annotation definition:
48964941 String genre();
48974942
48984943 Format format();
4899-
49004944 }
49014945----
49024946
@@ -5378,7 +5422,7 @@ comma/semicolon/space-separated list that includes the parent package of each cl
53785422
53795423[NOTE]
53805424====
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
53825426annotation, i.e. `@ComponentScan("org.example")`
53835427====
53845428
@@ -5427,7 +5471,7 @@ wired together - all without any bean configuration metadata provided in XML.
54275471====
54285472You can disable the registration of `AutowiredAnnotationBeanPostProcessor` and
54295473`CommonAnnotationBeanPostProcessor` by including the __annotation-config__ attribute
5430- with a value of false.
5474+ with a value of ` false` .
54315475====
54325476
54335477
@@ -5865,6 +5909,7 @@ metadata is provided per-instance rather than per-class.
58655909
58665910[[beans-standard-annotations]]
58675911== Using JSR 330 Standard Annotations
5912+
58685913Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations
58695914(Dependency Injection). Those annotations are scanned in the same way as the Spring
58705915annotations. You just need to have the relevant jars in your classpath.
@@ -5964,6 +6009,34 @@ you should use the `@Named` annotation as follows:
59646009 }
59656010----
59666011
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+
59676040
59686041
59696042[[beans-named]]
@@ -6712,6 +6785,7 @@ annotation can be used:
67126785----
67136786
67146787
6788+
67156789[[beans-java-configuration-annotation]]
67166790=== Using the @Configuration annotation
67176791
@@ -7949,7 +8023,7 @@ AspectJ load-time weaving, see <<aop-aj-ltw>>.
79498023
79508024
79518025[[context-introduction]]
7952- == Additional Capabilities of the ApplicationContext
8026+ == Additional capabilities of the ApplicationContext
79538027
79548028As was discussed in the chapter introduction, the `org.springframework.beans.factory`
79558029package provides basic functionality for managing and manipulating beans, including in a
@@ -8181,7 +8255,7 @@ out the `ReloadableResourceBundleMessageSource` javadocs for details.
81818255
81828256
81838257[[context-functionality-events]]
8184- === Standard and Custom Events
8258+ === Standard and custom events
81858259
81868260Event handling in the `ApplicationContext` is provided through the `ApplicationEvent`
81878261class and `ApplicationListener` interface. If a bean that implements the
@@ -8370,8 +8444,9 @@ http://www.enterpriseintegrationpatterns.com[pattern-oriented], event-driven
83708444architectures that build upon the well-known Spring programming model.
83718445====
83728446
8447+
83738448[[context-functionality-events-annotation]]
8374- ==== Annotation-based Event Listeners
8449+ ==== Annotation-based event listeners
83758450
83768451As of Spring 4.2, an event listener can be registered on any public method of a managed
83778452bean 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`
84788553by the method above. If you need to publish several events, just return a `Collection` of
84798554events instead.
84808555
8556+
84818557[[context-functionality-events-async]]
84828558==== Asynchronous Listeners
84838559
@@ -8504,7 +8580,7 @@ Be aware of the following limitations when using asynchronous events:
85048580
85058581
85068582[[context-functionality-events-order]]
8507- ==== Ordering Listeners
8583+ ==== Ordering listeners
85088584
85098585If you need the listener to be invoked before another one, just add the `@Order`
85108586annotation to the method declaration:
@@ -8519,8 +8595,9 @@ annotation to the method declaration:
85198595 }
85208596----
85218597
8598+
85228599[[context-functionality-events-generics]]
8523- ==== Generic Events
8600+ ==== Generic events
85248601
85258602You may also use generics to further define the structure of your event. Consider an
85268603`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.
86868763
86878764[[beans-beanfactory]]
86888765== The BeanFactory
8766+
86898767The `BeanFactory` provides the underlying basis for Spring's IoC functionality but it is
86908768only used directly in integration with other third-party frameworks and is now largely
86918769historical in nature for most users of Spring. The `BeanFactory` and related interfaces,
86928770such as `BeanFactoryAware`, `InitializingBean`, `DisposableBean`, are still present in
86938771Spring for the purposes of backward compatibility with the large number of third-party
86948772frameworks 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.
86978775
86988776This section provides additional background into the differences between the
86998777`BeanFactory` and `ApplicationContext` and how one might access the IoC container
0 commit comments