
Beans and its Lifecycle in Spring
A Spring IoC container manages one or more beans. As we have seen these beans are created using the configuration metadata that has been supplied by xml file (in our case it is spring-beans.xml ). The following steps is gone through in the process of instantiating a bean using Bean Factory and ApplicationContext Interface.
| Bean Factory Interface | Application Context Interface |
|---|---|
| Spring instantiates the bean. | Spring instantiates the bean. |
| Spring injects the bean’s properties. | Spring injects the bean’s properties. |
| If the bean implements BeanNameAware, Spring passes the bean’s ID to setBeanName(). | If the bean implements BeanNameAware, Spring passes the bean’s ID to setBeanName(). |
| If the bean implements BeanFactoryAware, Spring passes the bean factory to setBeanFactory(). | If the bean implements BeanFactoryAware, Spring passes the bean factory to setBeanFactory(). |
| If there are any BeanPostProcessors, Spring calls their postProcessBeforeInitialization() method. | If the bean implements ApplicationContextAware interface, the setApplicationContext() method is gets called. |
| If the bean implements InitializingBean, its afterPropertiesSet() method will be called. If the bean has a custom init method declared, the specified initialization method will be called. | If there are any BeanPostProcessors, Spring calls their postProcessBeforeInitialization() method. |
| If there are any BeanPostProcessors, Spring calls their postProcessAfterInitialization() method. | If the bean implements InitializingBean, its afterPropertiesSet() method will be called. If the bean has a custom init method declared, the specified initialization method will be called. |
| At this point the bean is ready to be used by the application and will remain in the bean factory until it is no longer needed. | If there are any BeanPostProcessors, Spring calls their postProcessAfterInitialization() method. |
| If the bean implements DisposableBean, its destroy() method will be called. If the bean has a custom destroy-method declared, the specified method will be called. | At this point the bean is ready to be used by the application and will remain in the bean factory until it is no longer needed. |
| If the bean implements DisposableBean, its destroy() method will be called. If the bean has a custom destroy-method declared, the specified method will be called. |
Note:-The only difference here is that if the bean implements the ApplicationContextAware interface, the setApplicationContext() method is called. We will cover all lifecycle methods in detail in coming sections.
Java Discussion
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver
- - Setting tab order in swin



