📘 Premium Read: Access my best content on
Medium member-only articles
— deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed.
Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount):
Explore My Udemy Courses
— Learn through real-time, project-based development.
When a Spring application starts, it goes through a series of steps to create, configure, and manage the lifecycle of its beans. Understanding this lifecycle is critical for building robust, maintainable, and resource-efficient Spring applications.
In this article, we’ll walk through the Spring bean lifecycle, explain the key lifecycle methods, and demonstrate each with clear examples and best practices.
What Is the Spring Bean Lifecycle?
A Spring bean goes through multiple phases from the moment it’s instantiated to the point it’s destroyed. These phases include:
Instantiation
Property Injection
Initialization
Use (Application logic)
Destruction
Spring provides various hooks and interfaces that allow you to run custom logic at different points in this lifecycle.
🧬 Common Lifecycle Methods in Spring
Let’s look at each one in detail.
1. Using @PostConstruct for Initialization
This annotation is used on a method that should run after the bean is constructed and dependencies are injected.
Example:
@Component publicclassEmailService {
@PostConstruct public void init() { System.out.println("EmailService initialized"); } }
Best Practice:
Use @PostConstruct for lightweight, dependency-based initialization logic like setting defaults, logging, or preloading data.
2. Implementing InitializingBean
If you want more control or avoid annotations, implement InitializingBean.
Use when annotation-free or interface-based design is preferred.
6. Using Custom destroyMethod in @Bean
Configure a destroy method in a @Bean definition.
Example:
@Configuration publicclassShutdownConfig {
@Bean(destroyMethod = "shutdown") public TaskProcessor taskProcessor() { return new TaskProcessor(); } }
publicclassTaskProcessor { publicvoidshutdown() { System.out.println("TaskProcessor shutting down cleanly..."); } }
Best Practice:
Useful for external libraries where annotations and interfaces can’t be added.
🧠Lifecycle Flow Recap (Execution Order)
Constructor called
Dependencies injected
@PostConstruct method called
afterPropertiesSet() (if implemented)
Custom initMethod (if defined)
Application runs…
@PreDestroy called on shutdown
destroy() method (if implemented)
Custom destroyMethod (if defined)
🧠Real-World Use Cases
Best Practices for Bean Lifecycle Management
Prefer @PostConstruct and @PreDestroy for modern Spring apps.
Use @Bean(initMethod = "...", destroyMethod = "...") for external classes.
Avoid using multiple lifecycle mechanisms in the same class.
Don’t perform heavy operations (like API calls or blocking IO) directly in lifecycle methods — consider background tasks.
✅ Conclusion
Spring provides multiple ways to hook into the bean lifecycle, giving you flexibility for both initialization and cleanup. Choose the right one based on your use case:
Annotations (@PostConstruct, @PreDestroy) → Clean and easy for modern apps.
Interfaces (InitializingBean, DisposableBean) → Useful for frameworks or legacy code.
@Bean methods with initMethod/destroyMethod → Great for third-party classes.
Understanding lifecycle methods helps you write robust, maintainable, and resource-efficient Spring applications.
Related Spring Boot and Microservices Tutorials/Guides:
Comments
Post a Comment
Leave Comment