@@ -1097,7 +1097,8 @@ load an entire Spring IoC container instance.
1097
1097
Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to
1098
1098
use constructors for mandatory dependencies and setter methods or configuration methods
1099
1099
for optional dependencies. Note that use of the <<beans-required-annotation,@Required>>
1100
- annotation on a setter method can be used to make the property be a required dependency.
1100
+ annotation on a setter method can be used to make the property be a required dependency;
1101
+ however, constructor injection with programmatic validation of arguments is preferable.
1101
1102
1102
1103
The Spring team generally advocates constructor injection, as it lets you implement
1103
1104
application components as immutable objects and ensures that required dependencies
@@ -1129,8 +1130,8 @@ The container performs bean dependency resolution as follows:
1129
1130
describes all the beans. Configuration metadata can be specified by XML, Java code, or
1130
1131
annotations.
1131
1132
* For each bean, its dependencies are expressed in the form of properties, constructor
1132
- arguments, or arguments to the static-factory method( if you use that instead of
1133
- a normal constructor). These dependencies are provided to the bean, when the bean is
1133
+ arguments, or arguments to the static-factory method ( if you use that instead of a
1134
+ normal constructor). These dependencies are provided to the bean, when the bean is
1134
1135
actually created.
1135
1136
* Each property or constructor argument is an actual definition of the value to set, or
1136
1137
a reference to another bean in the container.
@@ -3251,20 +3252,20 @@ of a bean. This section groups them as follows:
3251
3252
[[beans-factory-lifecycle]]
3252
3253
=== Lifecycle Callbacks
3253
3254
3254
- To interact with the container's management of the bean lifecycle, you can implement the
3255
- Spring `InitializingBean` and `DisposableBean` interfaces. The container calls
3255
+ To interact with the container's management of the bean lifecycle, you can implement
3256
+ the Spring `InitializingBean` and `DisposableBean` interfaces. The container calls
3256
3257
`afterPropertiesSet()` for the former and `destroy()` for the latter to let the bean
3257
3258
perform certain actions upon initialization and destruction of your beans.
3258
3259
3259
3260
[TIP]
3260
3261
====
3261
3262
The JSR-250 `@PostConstruct` and `@PreDestroy` annotations are generally considered best
3262
3263
practice for receiving lifecycle callbacks in a modern Spring application. Using these
3263
- annotations means that your beans are not coupled to Spring-specific interfaces. For
3264
- details, see <<beans-postconstruct-and-predestroy-annotations>>.
3264
+ annotations means that your beans are not coupled to Spring-specific interfaces.
3265
+ For details, see <<beans-postconstruct-and-predestroy-annotations>>.
3265
3266
3266
3267
If you do not want to use the JSR-250 annotations but you still want to remove
3267
- coupling, consider using of `init-method` and `destroy-method` object definition metadata.
3268
+ coupling, consider `init-method` and `destroy-method` bean definition metadata.
3268
3269
====
3269
3270
3270
3271
Internally, the Spring Framework uses `BeanPostProcessor` implementations to process any
@@ -3280,7 +3281,6 @@ startup and shutdown process, as driven by the container's own lifecycle.
3280
3281
The lifecycle callback interfaces are described in this section.
3281
3282
3282
3283
3283
-
3284
3284
[[beans-factory-lifecycle-initializingbean]]
3285
3285
==== Initialization Callbacks
3286
3286
@@ -3323,8 +3323,8 @@ no-argument signature. With Java configuration, you can use the `initMethod` att
3323
3323
----
3324
3324
====
3325
3325
3326
- The preceding example has almost exactly the same effect as the following example (which
3327
- consists of two listings):
3326
+ The preceding example has almost exactly the same effect as the following example
3327
+ (which consists of two listings):
3328
3328
3329
3329
====
3330
3330
[source,xml,indent=0]
@@ -4557,11 +4557,18 @@ example:
4557
4557
This annotation indicates that the affected bean property must be populated at
4558
4558
configuration time, through an explicit property value in a bean definition or through
4559
4559
autowiring. The container throws an exception if the affected bean property has not been
4560
- populated. This allows for eager and explicit failure, avoiding `NullPointerException` instances
4561
- or the like later on. We still recommend that you put assertions into the bean
4562
- class itself (for example, into an init method). Doing so enforces those required
4560
+ populated. This allows for eager and explicit failure, avoiding `NullPointerException`
4561
+ instances or the like later on. We still recommend that you put assertions into the
4562
+ bean class itself (for example, into an init method). Doing so enforces those required
4563
4563
references and values even when you use the class outside of a container.
4564
4564
4565
+ [NOTE]
4566
+ ====
4567
+ The `@Required` annotation is formally deprecated as of Spring Framework 5.1, in favor
4568
+ of using constructor injection for required settings (or a custom implementation of
4569
+ `InitializingBean.afterPropertiesSet()` along with bean property setter methods).
4570
+ ====
4571
+
4565
4572
4566
4573
4567
4574
[[beans-autowired-annotation]]
@@ -5451,14 +5458,14 @@ attribute set to `true`, it is selected.
5451
5458
[[beans-resource-annotation]]
5452
5459
=== Injection with `@Resource`
5453
5460
5454
- Spring also supports injection by using the JSR-250 `@Resource` annotation on fields or
5455
- bean property setter methods. This is a common pattern in Java EE 5 and 6 (for example,
5456
- in JSF 1.2 managed beans or JAX-WS 2.0 endpoints). Spring supports this pattern for
5457
- Spring-managed objects as well.
5461
+ Spring also supports injection by using the JSR-250 `@Resource` annotation
5462
+ (`javax.annotation.Resource`) on fields or bean property setter methods.
5463
+ This is a common pattern in Java EE: for example, in JSF-managed beans and JAX-WS
5464
+ endpoints. Spring supports this pattern for Spring-managed objects as well.
5458
5465
5459
- `@Resource` takes a name attribute. By default, Spring interprets that value as the
5460
- bean name to be injected. In other words, it follows by-name semantics, as
5461
- demonstrated in the following example:
5466
+ `@Resource` takes a name attribute. By default, Spring interprets that value as
5467
+ the bean name to be injected. In other words, it follows by-name semantics,
5468
+ as demonstrated in the following example:
5462
5469
5463
5470
====
5464
5471
[source,java,indent=0]
@@ -5512,7 +5519,7 @@ and resolves well known resolvable dependencies: the `BeanFactory`,
5512
5519
interfaces.
5513
5520
5514
5521
Thus, in the following example, the `customerPreferenceDao` field first looks for a bean
5515
- named customerPreferenceDao and then falls back to a primary type match for the type
5522
+ named " customerPreferenceDao" and then falls back to a primary type match for the type
5516
5523
`CustomerPreferenceDao`:
5517
5524
5518
5525
====
@@ -5543,15 +5550,16 @@ named customerPreferenceDao and then falls back to a primary type match for the
5543
5550
=== Using `@PostConstruct` and `@PreDestroy`
5544
5551
5545
5552
The `CommonAnnotationBeanPostProcessor` not only recognizes the `@Resource` annotation
5546
- but also the JSR-250 lifecycle annotations. Introduced in Spring 2.5, the support
5547
- for these annotations offers yet another alternative to those described in
5553
+ but also the JSR-250 lifecycle annotations: `javax.annotation.PostConstruct` and
5554
+ `javax.annotation.PreDestroy`. Introduced in Spring 2.5, the support for these
5555
+ annotations offers an alternative to the lifecycle callback mechanism described in
5548
5556
<<beans-factory-lifecycle-initializingbean,initialization callbacks>> and
5549
5557
<<beans-factory-lifecycle-disposablebean,destruction callbacks>>. Provided that the
5550
- `CommonAnnotationBeanPostProcessor` is registered within the Spring
5551
- `ApplicationContext`, a method carrying one of these annotations is invoked at the same
5552
- point in the lifecycle as the corresponding Spring lifecycle interface method or
5553
- explicitly declared callback method. In the following example, the cache is
5554
- pre-populated upon initialization and cleared upon destruction:
5558
+ `CommonAnnotationBeanPostProcessor` is registered within the Spring `ApplicationContext`,
5559
+ a method carrying one of these annotations is invoked at the same point in the lifecycle
5560
+ as the corresponding Spring lifecycle interface method or explicitly declared callback
5561
+ method. In the following example, the cache is pre-populated upon initialization and
5562
+ cleared upon destruction:
5555
5563
5556
5564
====
5557
5565
[source,java,indent=0]
@@ -5572,9 +5580,19 @@ pre-populated upon initialization and cleared upon destruction:
5572
5580
----
5573
5581
====
5574
5582
5575
- NOTE: For details about the effects of combining various lifecycle mechanisms, see
5583
+ For details about the effects of combining various lifecycle mechanisms, see
5576
5584
<<beans-factory-lifecycle-combined-effects>>.
5577
5585
5586
+ [NOTE]
5587
+ ====
5588
+ Like `@Resource`, the `@PostConstruct` and `@PreDestroy` annotation types were a part
5589
+ of the standard Java libraries from JDK 6 to 8. However, the entire `javax.annotation`
5590
+ package got separated from the core Java modules in JDK 9 and eventually removed in
5591
+ JDK 11. If needed, the `javax.annotation-api` artifact needs to be obtained via Maven
5592
+ Central now, simply to be added to the application's classpath like any other library.
5593
+ ====
5594
+
5595
+
5578
5596
5579
5597
5580
5598
[[beans-classpath-scanning]]
@@ -7115,8 +7133,8 @@ The following example shows how to prevent an automatic destruction callback for
7115
7133
----
7116
7134
====
7117
7135
7118
- Also, with `@Bean` methods, you typically use programmatic JNDI lookups,
7119
- either by using Spring's `JndiTemplate` or `JndiLocatorDelegate` helpers or straight JNDI
7136
+ Also, with `@Bean` methods, you typically use programmatic JNDI lookups, either by
7137
+ using Spring's `JndiTemplate` or `JndiLocatorDelegate` helpers or straight JNDI
7120
7138
`InitialContext` usage but not the `JndiObjectFactoryBean` variant (which would force
7121
7139
you to declare the return type as the `FactoryBean` type instead of the actual target
7122
7140
type, making it harder to use for cross-reference calls in other `@Bean` methods that
@@ -7187,7 +7205,7 @@ Spring offers a convenient way of working with scoped dependencies through
7187
7205
<<beans-factory-scopes-other-injection,scoped proxies>>. The easiest way to create such
7188
7206
a proxy when using the XML configuration is the `<aop:scoped-proxy/>` element.
7189
7207
Configuring your beans in Java with a `@Scope` annotation offers equivalent support with
7190
- the `proxyMode` attribute. The default is no proxy ( `ScopedProxyMode.NO`), but you can
7208
+ the `proxyMode` attribute. The default is no proxy (`ScopedProxyMode.NO`), but you can
7191
7209
specify `ScopedProxyMode.TARGET_CLASS` or `ScopedProxyMode.INTERFACES`.
7192
7210
7193
7211
If you port the scoped proxy example from the XML reference documentation (see
@@ -9364,9 +9382,9 @@ exist, the listener uses `/WEB-INF/applicationContext.xml` as a default. When th
9364
9382
parameter does exist, the listener separates the `String` by using predefined
9365
9383
delimiters (comma, semicolon, and whitespace) and uses the values as locations where
9366
9384
application contexts are searched. Ant-style path patterns are supported as well.
9367
- Examples are `/WEB-INF/{asterisk}Context.xml` (for all files with names that end with `Context.xml`
9368
- and that reside in the `WEB-INF` directory) and `/WEB-INF/**/*Context.xml`( for all such files
9369
- in any subdirectory of `WEB-INF`).
9385
+ Examples are `/WEB-INF/{asterisk}Context.xml` (for all files with names that end with
9386
+ `Context.xml` and that reside in the `WEB-INF` directory) and `/WEB-INF/**/*Context.xml`
9387
+ (for all such files in any subdirectory of `WEB-INF`).
9370
9388
9371
9389
9372
9390
@@ -9377,17 +9395,17 @@ It is possible to deploy a Spring `ApplicationContext` as a RAR file, encapsulat
9377
9395
context and all of its required bean classes and library JARs in a Java EE RAR deployment
9378
9396
unit. This is the equivalent of bootstrapping a stand-alone `ApplicationContext` (only hosted
9379
9397
in Java EE environment) being able to access the Java EE servers facilities. RAR deployment
9380
- is a more natural alternative to a scenario of deploying a headless WAR file -- in effect, a WAR
9381
- file without any HTTP entry points that is used only for bootstrapping a Spring
9398
+ is a more natural alternative to a scenario of deploying a headless WAR file -- in effect,
9399
+ a WAR file without any HTTP entry points that is used only for bootstrapping a Spring
9382
9400
`ApplicationContext` in a Java EE environment.
9383
9401
9384
9402
RAR deployment is ideal for application contexts that do not need HTTP entry points but
9385
9403
rather consist only of message endpoints and scheduled jobs. Beans in such a context can
9386
9404
use application server resources such as the JTA transaction manager and JNDI-bound JDBC
9387
- `DataSource` instances and JMS `ConnectionFactory` instances and can also register with the
9388
- platform's JMX server -- all through Spring's standard transaction management and JNDI
9389
- and JMX support facilities. Application components can also interact with the
9390
- application server's JCA `WorkManager` through Spring's `TaskExecutor` abstraction.
9405
+ `DataSource` instances and JMS `ConnectionFactory` instances and can also register with
9406
+ the platform's JMX server -- all through Spring's standard transaction management and JNDI
9407
+ and JMX support facilities. Application components can also interact with the application
9408
+ server's JCA `WorkManager` through Spring's `TaskExecutor` abstraction.
9391
9409
9392
9410
See the javadoc of the
9393
9411
{api-spring-framework}/jca/context/SpringContextResourceAdapter.html[`SpringContextResourceAdapter`]
@@ -9406,13 +9424,13 @@ and the corresponding Spring XML bean definition file(s) (typically
9406
9424
. Drop the resulting RAR file into your
9407
9425
application server's deployment directory.
9408
9426
9409
- NOTE: Such RAR deployment units are usually self-contained. They do not expose components to
9410
- the outside world, not even to other modules of the same application. Interaction with a
9427
+ NOTE: Such RAR deployment units are usually self-contained. They do not expose components
9428
+ to the outside world, not even to other modules of the same application. Interaction with a
9411
9429
RAR-based `ApplicationContext` usually occurs through JMS destinations that it shares with
9412
9430
other modules. A RAR-based `ApplicationContext` may also, for example, schedule some jobs
9413
9431
or react to new files in the file system (or the like). If it needs to allow synchronous
9414
- access from the outside, it could (for example) export RMI endpoints, which may
9415
- be used by other application modules on the same machine.
9432
+ access from the outside, it could (for example) export RMI endpoints, which may be used
9433
+ by other application modules on the same machine.
9416
9434
9417
9435
9418
9436
0 commit comments