You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: framework-docs/modules/ROOT/pages/core/beans/factory-nature.adoc
+57-21Lines changed: 57 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,7 @@ startup and shutdown process, as driven by the container's own lifecycle.
42
42
The lifecycle callback interfaces are described in this section.
43
43
44
44
45
+
45
46
[[beans-factory-lifecycle-initializingbean]]
46
47
=== Initialization Callbacks
47
48
@@ -132,6 +133,30 @@ Kotlin::
132
133
133
134
However, the first of the two preceding examples does not couple the code to Spring.
134
135
136
+
[NOTE]
137
+
====
138
+
Be aware that `@PostConstruct` and initialization methods in general are executed
139
+
within the container's singleton creation lock. The bean instance is only considered
140
+
as fully initialized and ready to be published to others after returning from the
141
+
`@PostConstruct` method. Such individual initialization methods are only meant
142
+
for validating the configuration state and possibly preparing some data structures
143
+
based on the given configuration but no further activity with external bean access.
144
+
Otherwise there is a risk for an initialization deadlock.
145
+
146
+
For a scenario where expensive post-initialization activity is to be triggered,
147
+
e.g. asynchronous database preparation steps, your bean should either implement
148
+
`SmartInitializingSingleton.afterSingletonsInstantiated()` or rely on the context
149
+
refresh event: implementing `ApplicationListener<ContextRefreshedEvent>` or
150
+
declaring its annotation equivalent `@EventListener(ContextRefreshedEvent.class)`.
151
+
Those variants come after all regular singleton initialization and therefore
152
+
outside of any singleton creation lock.
153
+
154
+
Alternatively, you may implement the `(Smart)Lifecycle` interface and integrate with
155
+
the container's overall lifecycle management, including an auto-startup mechanism,
156
+
a pre-destroy stop step, and potential stop/restart callbacks (see below).
157
+
====
158
+
159
+
135
160
136
161
[[beans-factory-lifecycle-disposablebean]]
137
162
=== Destruction Callbacks
@@ -223,31 +248,41 @@ Kotlin::
223
248
However, the first of the two preceding definitions does not couple the code to Spring.
224
249
225
250
TIP: You can assign the `destroy-method` attribute of a `<bean>` element a special
226
-
`(inferred)` value, which instructs Spring to automatically detect a public `close` or
227
-
`shutdown` method on the specific bean class. (Any class that implements
228
-
`java.lang.AutoCloseable` or `java.io.Closeable` would therefore match.) You can also set
229
-
this special `(inferred)` value on the `default-destroy-method` attribute of a
251
+
`(inferred)` value, which instructs Spring to automatically detect a public `close`
252
+
or `shutdown` method on the specific bean class. (Any class that implements
253
+
`java.lang.AutoCloseable` or `java.io.Closeable` would therefore match.) You can also
254
+
set this special `(inferred)` value on the `default-destroy-method` attribute of a
230
255
`<beans>` element to apply this behavior to an entire set of beans (see
231
-
xref:core/beans/factory-nature.adoc#beans-factory-lifecycle-default-init-destroy-methods[Default Initialization and Destroy Methods]). Note that this is the
232
-
default behavior with Java configuration.
256
+
xref:core/beans/factory-nature.adoc#beans-factory-lifecycle-default-init-destroy-methods[Default Initialization and Destroy Methods]).
257
+
Note that this is the default behavior for `@Bean` methods in Java configuration classes.
258
+
259
+
[NOTE]
260
+
====
261
+
For extended shutdown phases, you may implement the `Lifecycle` interface and receive
262
+
an early stop signal before the destroy methods of any singleton beans are called.
263
+
You may also implement `SmartLifecycle` for a time-bound stop step where the container
264
+
will wait for all such stop processing to complete before moving on to destroy methods.
0 commit comments