09 - Spring Bean Lifecycle
- 01 - What is Spring? What Problems Does It Solve?
- 02 - Spring vs Spring Boot: Understanding the Difference
- 03 - Spring MVC vs Spring Boot: When and Why to Use Each
- 04 - Installing Java, Maven, & IDE Setup (STS, Eclipse, IntelliJ)
- 05 - Running Your First Spring Boot Application
- 06 - Inversion of Control (IoC) in Spring
- 07 - Dependency Injection (DI) in Spring
- 08 - BeanFactory vs ApplicationContext
- 09 - Spring Bean LifecycleCurrent
- 10 - Bean Scopes: Singleton, Prototype & Custom Scopes
The Spring Bean Lifecycle is one of the most important topics in the Spring Framework. Understanding how beans are created, initialized, and destroyed helps you write cleaner, more predictable, and configurable applications.
If IoC is the heart of Spring, then the Bean Lifecycle is the rhythm that keeps it alive.
1. What Is a Spring Bean?
A Bean is an object managed by the Spring IoC Container.
Spring controls:
- How beans are created
- How dependencies are injected
- When beans are initialized
- How long they live
- How they are destroyed
Spring manages everything for you — you only define the logic.
2. Overview of the Spring Bean Lifecycle
Spring processes a bean through the following steps:
- Instantiate the bean
- Populate Properties (DI injection)
- Call BeanNameAware (if implemented)
- Call BeanFactoryAware (if implemented)
- Call ApplicationContextAware (if implemented)
- Before Initialization — BeanPostProcessor (pre-processing)
- Initialize Bean (
@PostConstruct, init-method) - After Initialization — BeanPostProcessor (post-processing)
- Bean Is Ready for Use
- Destroy Bean (
@PreDestroy, destroy-method)
Spring performs these steps automatically for every bean.
3. Bean Lifecycle Flow Diagram
Instantiate → Dependency Injection → Aware Interfaces →
BeanPostProcessor (before init) → Init Methods →
BeanPostProcessor (after init) → Ready → Destroy
4. Initialization Callbacks
Spring supports multiple ways to execute code right after a bean is created.
4.1 Using @PostConstruct
@Component
public class MyService {
@PostConstruct
public void init() {
System.out.println("Bean initialized!");
}
}
4.2 Using InitializingBean Interface
@Component
public class MyService implements InitializingBean {
@Override
public void afterPropertiesSet() {
System.out.println("Initialized using InitializingBean");
}
}
4.3 Using init-method in Configuration
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public MyService myService() {
return new MyService();
}
}
5. Destruction Callbacks
Called when the application context is shutting down.
5.1 Using @PreDestroy
@PreDestroy
public void cleanup() {
System.out.println("Cleaning up resources...");
}
5.2 Using DisposableBean
public class MyService implements DisposableBean {
@Override
public void destroy() {
System.out.println("Destroyed using DisposableBean");
}
}
5.3 Using destroy-method
@Bean(destroyMethod = "cleanup")
public MyService myService() {
return new MyService();
}
6. BeanPostProcessor in the Lifecycle
BeanPostProcessor lets you modify beans before and after initialization.
Example:
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println("Before Init: " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
System.out.println("After Init: " + beanName);
return bean;
}
}
This is widely used by Spring internally (for annotations like @Autowired and @Transactional).
7. Singleton vs Prototype Lifecycle
Singleton Beans (default)
- Created only once
- Destroy method is called
Prototype Beans
- Created every time requested
- Destroy method is NOT called automatically
Spring Boot primarily uses singleton beans.
8. Bean Lifecycle in Spring Boot
Spring Boot enhances lifecycle management with:
- Auto-configuration
- Automatic BeanPostProcessors
- Environment initialization
- Lazy initialization support
Startup logs show Spring creating and initializing beans.
9. Real-World Use Cases
Use @PostConstruct for:
- Loading initial data
- Validating configuration
- Opening resources
Use @PreDestroy for:
- Closing database connections
- Stopping background threads
- Cleaning up temp files
10. Summary
- The Bean Lifecycle defines how Spring creates, initializes, and destroys beans.
- Initialization and destruction methods allow custom logic.
- BeanPostProcessor allows modification of beans.
- Singleton and prototype beans behave differently.
- Spring Boot automatically manages most lifecycle tasks.
Understanding the lifecycle prepares you for deeper topics like scopes, proxies, and Spring AOP.
What's Next?
Coming up next:
10 - Bean Scopes: Singleton, Prototype & Custom Scopes
We'll explore how Spring manages bean instances across the application.
