SPRING CONCEPTS

Bean lifecycle callbacks

Spring Concepts / Bean lifecycle callbacks

Lifecycle callbacks run during managed initialization and supported destruction.

Understand the idea

PostConstruct follows construction and dependency injection. PreDestroy runs when a managed singleton is destroyed during normal context shutdown. Prototype beans receive initialization callbacks, but their destruction callbacks are not automatically invoked by Spring. Forced process termination may prevent cleanup.

A concrete example

@PostConstruct void initialize() { }
@PreDestroy void close() { }

In the application

See the independently runnable examples in Stage 23.

Calling new yourself does not invoke these callbacks automatically.

See it in a working stage

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly