๐ฑ๐ 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