@PreDestroy
An annotation marking a bean method to run while the application context is closing, to release resources the bean owns.
In more detail
It should close only what this bean opened, never a shared resource created elsewhere. Like @PostConstruct it comes from Jakarta Annotations.
Example
@PreDestroy void close() { }Related terms
- @PostConstruct — An annotation marking a bean method to run once, after dependency injection and before the bean is used.
- bean lifecycle — The stages a Spring bean passes through: construction, dependency injection, initialization, use, and destruction when the context closes.
- graceful shutdown — Stopping a service in an orderly way: finish current work, close connections, then exit.
Taught in
- The Spring Bean Lifecycle — Modern Java: Virtual Threads & High-Throughput Services