π±π Spring Bean Lifecycle — From Baby Bean to Farewell Party! π
Ever wondered how a Spring bean is born, grows up, works hard, and finally retires? Let’s take a fun, easy walk through its entire journey — perfect for freshers and curious developers! π
1️⃣ Bean Instantiation — “The Birth” πΆ
Easy words: Spring creates an empty object of your bean class.
@Component
public class MyBean {
public MyBean() {
System.out.println("Bean is born! πΆ");
}
}
What’s really happening: Spring uses reflection (Java’s ability to create objects without directly calling new) to make a new instance.
π Analogy: Like registering a newborn in a hospital — the baby is there, but not yet ready to work!
2️⃣ Dependency Injection — “Feeding the Baby” πΌ
Easy words: Spring injects the required data/services into your bean.
@Component
public class MyBean {
@Autowired
private MyService service;
}
What’s really happening: Spring checks the bean’s fields/constructors and injects matching beans from the container.
π Analogy: Like giving the baby food and clothes so they can survive!
3️⃣ Aware Interfaces — “Bean Learns About the World” π
Easy words: If your bean implements special interfaces, Spring will pass info like its name or context.
@Component
public class MyBean implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println("My bean name is: " + name);
}
}
What’s really happening: Spring calls these methods so your bean “knows” about the container.
π Analogy: Like telling the baby their name, home address, and city.
4️⃣ @PostConstruct / InitializingBean — “Getting Ready for Work” πΌ
Easy words: Spring calls your init method after all injections are done.
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean is ready to work!");
}
}
What’s really happening: Any method marked with @PostConstruct or afterPropertiesSet() is called before the bean is available for use.
π Analogy: Like the baby learning skills before starting the job.
5️⃣ Bean in Use — “Doing the Job” π
Easy words: The bean now serves requests in your application.
What’s really happening: The bean stays in the container, handling calls until the application shuts down.
π Analogy: Like an adult working daily to earn salary.
6️⃣ @PreDestroy / DisposableBean — “Retirement” π
Easy words: Before the container closes, Spring calls your cleanup method.
@Component
public class MyBean {
@PreDestroy
public void cleanup() {
System.out.println("Bean is cleaning up before retirement!");
}
}
What’s really happening: This is where you close connections, release resources, etc.
π Analogy: Like returning office laptop and saying goodbye on the last day.
π Spring Bean Lifecycle – Diagrammatic Representation
(Constructor)
(Setter Injection)
(BeanNameAware, BeanFactoryAware...)
π― Wrapping Up
Spring beans aren’t just “magically” there — they have a full life story. Understand these stages, and you’ll debug and design much better. π
π¬ Have you ever added a @PostConstruct and been shocked when it didn’t run? Share your story below!
Comments
Post a Comment