bean lifecycle
The stages a Spring bean passes through: construction, dependency injection, initialization, use, and destruction when the context closes.
In more detail
Put preparation in an initialization callback after injection, not in a constructor that may see incomplete framework state. Put cleanup in a destruction callback, and only for resources this bean owns. Order between beans follows their dependencies; explicit ordering should be rare.
Related terms
- @PostConstruct — An annotation marking a bean method to run once, after dependency injection and before the bean is used.
- @PreDestroy — An annotation marking a bean method to run while the application context is closing, to release resources the bean owns.
- Spring bean — An object that the Spring container creates, configures and manages, and can inject into other beans.
- 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