Spring Framework FAQ: Top Questions
9. What are Spring Bean lifecycle methods?
Spring beans go through several stages managed by the Spring container. Lifecycle callbacks allow you to hook into these stages for custom initialization and destruction.
📘 Key Methods:
InitializingBean.afterPropertiesSet()
DisposableBean.destroy()
@PostConstruct
and@PreDestroy
📥 Example:
@PostConstruct
public void init() {
System.out.println("Bean is initialized");
}
🏆 Expected Output:
Logs a message during bean initialization phase.
🛠️ Use Cases:
- Resource setup (e.g., DB connections).
- Graceful cleanup of resources.