Bean Lifecycle
Bean Lifecycle
container. It encompasses various stages that a bean goes through, from its
instantiation to its destruction. Here's an overview of the main phases of a Spring
bean's life cycle:
1. Instantiation
The Spring container creates an instance of the bean using either a constructor or
a factory method, depending on how the bean is defined in the configuration.
This occurs when the bean is requested for the first time or during application
startup (for eager singleton beans).
Example (constructor-based instantiation):
java
Copy code
public class MyBean {
public MyBean() {
System.out.println("Bean instantiated");
}
}
2. Dependency Injection (Populate Properties)
After instantiation, Spring performs dependency injection by setting the properties
of the bean (using constructor injection, setter injection, or field injection).
The bean's dependencies are resolved from the Spring container and injected.
Example:
java
Copy code
public class MyBean {
private Dependency dep;
@Autowired
public void setDependency(Dependency dep) {
this.dep = dep;
}
}
3. Bean Name Aware (Optional)
If the bean implements the BeanNameAware interface, Spring provides the bean's name
to the bean instance via the setBeanName() method.
This is optional and is used when the bean needs to know its ID or name in the
container.
Example:
java
Copy code
public class MyBean implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println("Bean name is: " + name);
}
}
4. Bean Factory Aware (Optional)
If the bean implements the BeanFactoryAware interface, Spring injects the
BeanFactory that created the bean.
This can be useful if the bean needs to interact with the container
programmatically.
Example:
java
Copy code
public class MyBean implements BeanFactoryAware {
@Override
public void setBeanFactory(BeanFactory beanFactory) {
System.out.println("BeanFactory set");
}
}
5. ApplicationContext Aware (Optional)
If the bean implements the ApplicationContextAware interface, Spring injects the
ApplicationContext into the bean.
This is used when the bean needs to interact with the ApplicationContext.
Example:
java
Copy code
public class MyBean implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext context) {
System.out.println("ApplicationContext set");
}
}
6. Pre-initialization: Bean Post Processors (Optional)
Spring allows custom logic before a bean is initialized through the
BeanPostProcessor interface. This occurs before the @PostConstruct method or custom
initialization methods are called.
This is useful for modifying or decorating beans.
Example:
java
Copy code
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
System.out.println("Before Initialization: " + beanName);
return bean;
}
}
7. Initialization
If the bean has an init method, it is invoked at this point.
There are two ways to define initialization logic:
Using @PostConstruct annotation: Called after the bean has been constructed and
dependency injection is complete.
Using a custom init method: Defined in the configuration, such as in XML or with
the @Bean annotation.
Example:
java
Copy code
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
Or custom init method:
java
Copy code
public class MyBean {
public void customInit() {
System.out.println("Custom Init Method called");
}
}
// XML Configuration
<bean id="myBean" class="com.example.MyBean" init-method="customInit"/>
8. Post-initialization: Bean Post Processors (Optional)
After the bean's initialization, Spring allows custom logic again through the
BeanPostProcessor interface via the postProcessAfterInitialization() method.
This is commonly used for bean enhancements or applying additional logic.
Example:
java
Copy code
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
System.out.println("After Initialization: " + beanName);
return bean;
}
}
9. Bean in Use
The bean is now fully initialized and ready to be used within the application. It
remains in this state as long as it is needed.
For singleton beans, the bean remains in the Spring container until the application
context is closed.
For prototype beans, the bean may be garbage collected when it is no longer in use.
10. Destruction
When the application shuts down or the bean is no longer needed, Spring invokes any
defined destruction logic. This happens for beans with a defined destroy method.
Like initialization, there are two ways to define destruction logic:
Using @PreDestroy annotation: Called before the bean is removed from the container.
Using a custom destroy method: Defined in the XML or with the @Bean annotation.
Example:
java
Copy code
@PreDestroy
public void destroy() {
System.out.println("Bean is about to be destroyed");
}
Or custom destroy method:
java
Copy code
public class MyBean {
public void customDestroy() {
System.out.println("Custom Destroy Method called");
}
}
// XML Configuration
<bean id="myBean" class="com.example.MyBean" destroy-method="customDestroy"/>
Lifecycle Summary:
Instantiation: Bean is created.
Dependency Injection: Dependencies are injected.
Aware Interfaces (Optional): Container injects specific resources (e.g.,
BeanFactory, ApplicationContext).
Pre-initialization: BeanPostProcessor processes before initialization.
Initialization: @PostConstruct or custom init methods are called.
Post-initialization: BeanPostProcessor processes after initialization.
Usage: The bean is ready for use.
Destruction: @PreDestroy or custom destroy methods are invoked before the bean is
destroyed.
Interace Qualifier
{
void run();
}
class A imp q{