The Spring Framework manages the lifecycle of Spring Beans, and it includes several stages from the creation of a bean to its destruction. Here are the stages:
1. Instantiation: This is the stage where the bean is created in the Spring container.
2. Populate Properties: In this stage, the properties specified in the bean definition are set in the bean.
3. Set Bean Name: If the bean implements `BeanNameAware`, Spring passes the bean's ID to `setBeanName()` method.
4. Set Bean Factory: If Bean implements `BeanFactoryAware`, Spring passes the beanfactory to `setBeanFactory()` method.
5. Pre Initialization - BeanPostProcessors: Before Spring initiates the initialization process, if there are any BeanPostProcessors associated with the bean, Spring calls `postProcessBeforeInitialization()` method.
6. Initialize Beans: If the bean implements `InitializingBean`, its `afterPropertiesSet()` method is called. If the bean has an init method declared, that is called.
7. Post Initialization - BeanPostProcessors: After Spring completes the bean initialization, if there are any BeanPostProcessors associated with the bean, Spring calls `postProcessAfterInitialization()` method.
8. Ready to use: Now, the bean is ready to be used by the application.
9. Destroy: If the bean implements `DisposableBean`, it will call the `destroy()` method.
1. Instantiation: This is the stage where the bean is created in the Spring container.
2. Populate Properties: In this stage, the properties specified in the bean definition are set in the bean.
3. Set Bean Name: If the bean implements `BeanNameAware`, Spring passes the bean's ID to `setBeanName()` method.
4. Set Bean Factory: If Bean implements `BeanFactoryAware`, Spring passes the beanfactory to `setBeanFactory()` method.
5. Pre Initialization - BeanPostProcessors: Before Spring initiates the initialization process, if there are any BeanPostProcessors associated with the bean, Spring calls `postProcessBeforeInitialization()` method.
6. Initialize Beans: If the bean implements `InitializingBean`, its `afterPropertiesSet()` method is called. If the bean has an init method declared, that is called.
7. Post Initialization - BeanPostProcessors: After Spring completes the bean initialization, if there are any BeanPostProcessors associated with the bean, Spring calls `postProcessAfterInitialization()` method.
8. Ready to use: Now, the bean is ready to be used by the application.
9. Destroy: If the bean implements `DisposableBean`, it will call the `destroy()` method.
Here is an example of a Spring Bean lifecycle:
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class ExampleBean implements InitializingBean, DisposableBean {
public void afterPropertiesSet() {
// Initialization code here
}
public void destroy() {
// Cleanup code here
}
}
In this example, `afterPropertiesSet()` is called after the bean's properties have been set, and `destroy()` is called when the bean is being destroyed.
Note:
Note:
@PostConstruct
: This annotation is used on a method that needs to be executed after the bean is fully initialized. This method is called just after Spring has set all the properties (dependencies) on the bean. It occurs after the afterPropertiesSet()
method (from InitializingBean
interface) and any declared init method.@PreDestroy
: This annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container. The method annotated with @PreDestroy
is typically used to release resources that the instance has been holding. This method is called just before the Spring container removes the bean from the context. It occurs before the destroy()
method (from DisposableBean
interface) and any declared destroy method.Reference: https://howtodoinjava.com/spring-core/spring-bean-life-cycle/
Post a Comment