Skip to content

Commit 5f4c461

Browse files
committed
Documentation revision for @PostConstruct/PreDestroy and @required
Closes gh-22348
1 parent ef86526 commit 5f4c461

File tree

1 file changed

+64
-46
lines changed

1 file changed

+64
-46
lines changed

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,8 @@ load an entire Spring IoC container instance.
10971097
Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to
10981098
use constructors for mandatory dependencies and setter methods or configuration methods
10991099
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.
11011102
11021103
The Spring team generally advocates constructor injection, as it lets you implement
11031104
application components as immutable objects and ensures that required dependencies
@@ -1129,8 +1130,8 @@ The container performs bean dependency resolution as follows:
11291130
describes all the beans. Configuration metadata can be specified by XML, Java code, or
11301131
annotations.
11311132
* 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
11341135
actually created.
11351136
* Each property or constructor argument is an actual definition of the value to set, or
11361137
a reference to another bean in the container.
@@ -3251,20 +3252,20 @@ of a bean. This section groups them as follows:
32513252
[[beans-factory-lifecycle]]
32523253
=== Lifecycle Callbacks
32533254

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
32563257
`afterPropertiesSet()` for the former and `destroy()` for the latter to let the bean
32573258
perform certain actions upon initialization and destruction of your beans.
32583259

32593260
[TIP]
32603261
====
32613262
The JSR-250 `@PostConstruct` and `@PreDestroy` annotations are generally considered best
32623263
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>>.
32653266
32663267
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.
32683269
====
32693270

32703271
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.
32803281
The lifecycle callback interfaces are described in this section.
32813282

32823283

3283-
32843284
[[beans-factory-lifecycle-initializingbean]]
32853285
==== Initialization Callbacks
32863286

@@ -3323,8 +3323,8 @@ no-argument signature. With Java configuration, you can use the `initMethod` att
33233323
----
33243324
====
33253325

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):
33283328

33293329
====
33303330
[source,xml,indent=0]
@@ -4557,11 +4557,18 @@ example:
45574557
This annotation indicates that the affected bean property must be populated at
45584558
configuration time, through an explicit property value in a bean definition or through
45594559
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
45634563
references and values even when you use the class outside of a container.
45644564

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+
45654572

45664573

45674574
[[beans-autowired-annotation]]
@@ -5451,14 +5458,14 @@ attribute set to `true`, it is selected.
54515458
[[beans-resource-annotation]]
54525459
=== Injection with `@Resource`
54535460

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.
54585465

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:
54625469

54635470
====
54645471
[source,java,indent=0]
@@ -5512,7 +5519,7 @@ and resolves well known resolvable dependencies: the `BeanFactory`,
55125519
interfaces.
55135520

55145521
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
55165523
`CustomerPreferenceDao`:
55175524

55185525
====
@@ -5543,15 +5550,16 @@ named customerPreferenceDao and then falls back to a primary type match for the
55435550
=== Using `@PostConstruct` and `@PreDestroy`
55445551

55455552
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
55485556
<<beans-factory-lifecycle-initializingbean,initialization callbacks>> and
55495557
<<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:
55555563

55565564
====
55575565
[source,java,indent=0]
@@ -5572,9 +5580,19 @@ pre-populated upon initialization and cleared upon destruction:
55725580
----
55735581
====
55745582

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
55765584
<<beans-factory-lifecycle-combined-effects>>.
55775585

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+
55785596

55795597

55805598
[[beans-classpath-scanning]]
@@ -7115,8 +7133,8 @@ The following example shows how to prevent an automatic destruction callback for
71157133
----
71167134
====
71177135
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
71207138
`InitialContext` usage but not the `JndiObjectFactoryBean` variant (which would force
71217139
you to declare the return type as the `FactoryBean` type instead of the actual target
71227140
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
71877205
<<beans-factory-scopes-other-injection,scoped proxies>>. The easiest way to create such
71887206
a proxy when using the XML configuration is the `<aop:scoped-proxy/>` element.
71897207
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
71917209
specify `ScopedProxyMode.TARGET_CLASS` or `ScopedProxyMode.INTERFACES`.
71927210

71937211
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
93649382
parameter does exist, the listener separates the `String` by using predefined
93659383
delimiters (comma, semicolon, and whitespace) and uses the values as locations where
93669384
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`).
93709388

93719389

93729390

@@ -9377,17 +9395,17 @@ It is possible to deploy a Spring `ApplicationContext` as a RAR file, encapsulat
93779395
context and all of its required bean classes and library JARs in a Java EE RAR deployment
93789396
unit. This is the equivalent of bootstrapping a stand-alone `ApplicationContext` (only hosted
93799397
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
93829400
`ApplicationContext` in a Java EE environment.
93839401

93849402
RAR deployment is ideal for application contexts that do not need HTTP entry points but
93859403
rather consist only of message endpoints and scheduled jobs. Beans in such a context can
93869404
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.
93919409

93929410
See the javadoc of the
93939411
{api-spring-framework}/jca/context/SpringContextResourceAdapter.html[`SpringContextResourceAdapter`]
@@ -9406,13 +9424,13 @@ and the corresponding Spring XML bean definition file(s) (typically
94069424
. Drop the resulting RAR file into your
94079425
application server's deployment directory.
94089426

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
94119429
RAR-based `ApplicationContext` usually occurs through JMS destinations that it shares with
94129430
other modules. A RAR-based `ApplicationContext` may also, for example, schedule some jobs
94139431
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.
94169434

94179435

94189436

0 commit comments

Comments
 (0)