What is Bean Lifecycle in Spring?
Bean Lifecycle in Spring refers to the complete process that a Spring Bean goes through from its creation to destruction inside the Spring IoC (Inversion of Control) container.
The Spring container is responsible for:
- Creating beans
- Injecting dependencies
- Initializing beans
- Managing bean usage
- Destroying beans
Understanding the Spring Bean Lifecycle is extremely important for:
- Spring Framework
- Spring Boot
- Enterprise Applications
- Microservices Architecture
- Cloud-Native Systems
Simple Definition
Bean Lifecycle is the sequence of steps followed by Spring while managing a bean from creation until destruction.
Real-Time Banking Example
Consider a banking application with:
- Payment Service
- Notification Service
- Fraud Detection Service
Spring performs the following operations:
- Creates service beans
- Injects dependencies
- Initializes services
- Uses services during execution
- Destroys services during shutdown
Complete Bean Lifecycle Flow
Spring Container Starts
|
Bean Instantiation
|
Dependency Injection
|
Bean Name Awareness
|
BeanFactory Awareness
|
Pre Initialization
|
Custom Initialization
|
Post Initialization
|
Bean Ready for Use
|
Application Running
|
Bean Destruction
|
Spring Container Shutdown
Main Phases of Bean Lifecycle
- Bean Instantiation
- Dependency Injection
- Bean Initialization
- Bean Usage
- Bean Destruction
1. Bean Instantiation
During this phase, Spring creates the bean object.
Example
@Service
public class PaymentService {
}
Spring creates the PaymentService object automatically.
2. Dependency Injection
After bean creation, Spring injects dependencies into the bean.
Example
@Service
public class PaymentService {
@Autowired
private NotificationService notificationService;
}
Spring injects NotificationService into PaymentService.
3. Bean Awareness Interfaces
Spring provides awareness interfaces to give beans information about the container environment.
Common awareness interfaces:
- BeanNameAware
- BeanFactoryAware
- ApplicationContextAware
BeanNameAware Example
public class PaymentService
implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println(name);
}
}
Spring provides the bean name automatically.
4. BeanPostProcessor - Pre Initialization
Spring calls BeanPostProcessor methods before bean initialization.
Method
postProcessBeforeInitialization()
BeanPostProcessor Example
@Component
public class CustomBeanProcessor
implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(
Object bean,
String beanName) {
return bean;
}
}
5. Bean Initialization
During initialization:
- Custom initialization logic executes
- Resources are initialized
- Database connections may open
Using @PostConstruct
@PostConstruct executes after dependency injection.
Example
@PostConstruct
public void init() {
System.out.println("Bean Initialized");
}
InitializingBean Interface
Spring provides InitializingBean interface for initialization callbacks.
Example
public class PaymentService
implements InitializingBean {
@Override
public void afterPropertiesSet() {
System.out.println("Initialization Done");
}
}
Custom init-method Example
@Bean(initMethod = "init")
public PaymentService paymentService() {
return new PaymentService();
}
6. BeanPostProcessor - Post Initialization
Spring calls BeanPostProcessor after initialization.
Method
postProcessAfterInitialization()
7. Bean Ready for Use
After initialization, the bean becomes ready for application use.
Example:
- REST APIs start handling requests
- Services start processing business logic
- Repositories start database operations
8. Bean Destruction
When the application shuts down:
- Spring destroys beans
- Resources are released
- Connections are closed
Using @PreDestroy
@PreDestroy executes before bean destruction.
Example
@PreDestroy
public void destroy() {
System.out.println("Bean Destroyed");
}
DisposableBean Interface
Spring provides DisposableBean interface for destruction callbacks.
Example
public class PaymentService
implements DisposableBean {
@Override
public void destroy() {
System.out.println("Cleanup Done");
}
}
Custom destroy-method Example
@Bean(destroyMethod = "cleanup")
public PaymentService paymentService() {
return new PaymentService();
}
Complete Bean Lifecycle Example
@Component
public class PaymentService {
public PaymentService() {
System.out.println("Bean Created");
}
@PostConstruct
public void init() {
System.out.println("Bean Initialized");
}
@PreDestroy
public void destroy() {
System.out.println("Bean Destroyed");
}
}
Output Sequence
Bean Created
Bean Initialized
Application Running
Bean Destroyed
Bean Lifecycle in Singleton Scope
Singleton beans:
- Created once per application
- Destroyed during application shutdown
Bean Lifecycle in Prototype Scope
Prototype beans:
- Created every time requested
- Spring does not manage destruction automatically
Why Bean Lifecycle is Important
Bean Lifecycle management helps:
- Resource management
- Database connection handling
- Cache initialization
- Message queue setup
- Performance optimization
Bean Lifecycle in Spring Boot
Spring Boot heavily uses bean lifecycle management for:
- REST APIs
- Database configuration
- Security initialization
- Microservices startup
- Cloud-native systems
Bean Lifecycle in Microservices
In Microservices Architecture:
- Many services start dynamically
- Dependencies must initialize correctly
- Graceful shutdown becomes important
Bean lifecycle management helps maintain reliability.
Microservices Banking Example
Payment Service Bean
|
Notification Service Bean
|
Fraud Detection Service Bean
|
Application Shutdown
|
Cleanup Resources
Advantages of Bean Lifecycle Management
- Automatic resource management
- Improved application reliability
- Better scalability
- Graceful shutdown support
- Efficient memory handling
Common Spring Annotations Related to Bean Lifecycle
- @Component
- @Bean
- @Autowired
- @PostConstruct
- @PreDestroy
- @Scope
Common Interview Questions
What is Bean Lifecycle in Spring?
Bean Lifecycle is the complete process followed by Spring for bean creation, dependency injection, initialization, usage, and destruction.
Which annotations are used in Bean Lifecycle?
Common annotations include @PostConstruct and @PreDestroy.
What is the difference between initialization and destruction methods?
Initialization methods execute after dependency injection, while destruction methods execute during application shutdown.
Professional Interview Answer
Bean Lifecycle in Spring refers to the sequence of operations performed by the Spring IoC container from bean creation to bean destruction. The lifecycle includes bean instantiation, dependency injection, initialization, bean usage, and destruction. Spring provides lifecycle callback mechanisms such as @PostConstruct, @PreDestroy, InitializingBean, and DisposableBean to execute custom logic during initialization and destruction phases.
Conclusion
Bean Lifecycle is one of the most important concepts in Spring Framework and Spring Boot.
It allows Spring to efficiently manage bean creation, dependency injection, initialization, resource handling, and graceful shutdown operations automatically.
Bean Lifecycle management is widely used in enterprise applications, banking systems, microservices architectures, and cloud-native systems to improve reliability, scalability, and maintainability.
Understanding Bean Lifecycle is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.