Spring Bean Lifecycle: Instantiation, Dependency Injection, Initialization, Destruction

🔹 Spring Concept: Bean Lifecycle In the Spring Framework, every object managed by the Spring IoC container is called a Bean. Understanding the Bean Lifecycle helps developers control how objects are created, initialized, and destroyed. 📌 Stages of Spring Bean Lifecycle 1️⃣ Instantiation Spring creates the bean instance. 2️⃣ Dependency Injection Spring injects required dependencies. 3️⃣ Initialization Custom initialization logic runs using: • @PostConstruct • InitializingBean • custom init-method 4️⃣ Bean Ready for Use The bean is now fully initialized and used in the application. 5️⃣ Destruction When the application context closes, cleanup logic runs using: • @PreDestroy • DisposableBean • custom destroy-method 💡 Example: @Component public class NotificationService {   @PostConstruct   public void init() {     System.out.println(“Bean Initialized”);   }   @PreDestroy   public void destroy() {     System.out.println(“Bean Destroyed”);   } } ✨ Understanding the Bean lifecycle helps developers manage resources efficiently and write better Spring applications. #Java #SpringBoot #SpringFramework #BackendDevelopment #SoftwareEngineering

To view or add a comment, sign in

Explore content categories