Skip to content

🌱 Spring Bean Lifecycle

Spring beans don’t just appear… they go through a carefully choreographed life journey 🎭 From birth β†’ preparation β†’ service β†’ retirement.

Let’s walk that lifecycle step by step.


πŸ”Ή 1. Bean Instantiation (Birth)

Spring creates the bean object.

@Component
class MyService {
    public MyService() {
        System.out.println("Constructor called");
    }
}

πŸ‘‰ Bean is born using constructor or factory method.


πŸ”Ή 2. Populate Properties (Dependency Injection)

Spring injects dependencies:

@Autowired
private AnotherService service;

πŸ‘‰ Like equipping the bean with tools 🧰


πŸ”Ή 3. Aware Interfaces (Optional)

Bean gets context awareness:

  • BeanNameAware
  • ApplicationContextAware
@Override
public void setBeanName(String name) {
    System.out.println("Bean name: " + name);
}

πŸ‘‰ β€œHey bean, you exist in this world, here’s your identity.”


πŸ”Ή 4. BeanPostProcessor (Before Init)

Custom logic before initialization:

public Object postProcessBeforeInitialization(Object bean, String name)

πŸ‘‰ Used for:

  • Logging
  • Proxy creation
  • Modifications

πŸ”Ή 5. Initialization Phase

Two main ways:

βœ… Using @PostConstruct

@PostConstruct
public void init() {
    System.out.println("Bean initialized");
}

βœ… Using InitializingBean

@Override
public void afterPropertiesSet() {
    System.out.println("After properties set");
}

πŸ‘‰ Bean is now fully ready πŸš€


πŸ”Ή 6. BeanPostProcessor (After Init)

public Object postProcessAfterInitialization(Object bean, String name)

πŸ‘‰ Often used for:

  • AOP proxies
  • Enhancements

πŸ”Ή 7. Bean Ready for Use

Now the bean is:

  • Fully initialized
  • Managed by Spring container

πŸ‘‰ This is where your business logic runs


πŸ”Ή 8. Destruction Phase (Shutdown)

When container shuts down:

βœ… Using @PreDestroy

@PreDestroy
public void destroy() {
    System.out.println("Bean destroyed");
}

βœ… Using DisposableBean

@Override
public void destroy() {
    System.out.println("Cleanup done");
}

πŸ‘‰ Cleanup resources (DB connections, threads, etc.)


πŸ”„ Full Lifecycle Flow

Instantiate β†’ Inject Dependencies β†’ Aware Interfaces β†’
BeanPostProcessor (Before) β†’ Init β†’
BeanPostProcessor (After) β†’ Ready β†’
Destroy

🎯 Interview Answer (Concise)

β€œSpring bean lifecycle includes instantiation, dependency injection, awareness callbacks, pre-initialization processing, initialization via @PostConstruct or InitializingBean, post-initialization processing, and finally destruction using @PreDestroy or DisposableBean.”


🧠 Real Insight (Advanced)

  • BeanPostProcessor is heavily used internally by Spring for:

    • AOP
    • Transactions
    • Security

πŸ‘‰ That’s how magic like @Transactional works ✨


⚠️ Common Mistakes

  • Forgetting that:

    • @PostConstruct runs after dependency injection
    • @PreDestroy only works for singleton beans

πŸš€ Memory Trick

Think of it like a startup employee πŸ‘¨β€πŸ’»

  1. Hired (instantiate)
  2. Given tools (DI)
  3. Orientation (aware)
  4. Training (init)
  5. Works (ready)
  6. Exit interview (destroy)