Bean's Life Cycle
- Instantiate Bean
- Populate properties in Bean
- BeanNameAware-setBeanName()
- BeanFactoryAware-setBeanFactory()
- ApplicationContextAware-setApplicationContext()
- Pre-initialization-BeanPostProcessors
- InitializingBean's-afterPropertiesSet()
- Call custom - init-method for bean
- Post-initialization-BeanPostProcessors
- Bean is ready to use
- Container is shut down
- DisposableBean’s-destroy()
- Call custom - destroy-method for bean
Detail
1) Spring container instantiates the bean
2) Spring container wires the bean references and populates values in the properties.3) If BeanNameAware interface is implemented by the bean then container sets the ID in the created bean using the setBeanName method
4) If BeanFactoryAware interface is implemented by bean then container sets the bean factory itselft in the created bean using the setBeanFactory method
5) If ApplicationContextAware interface is implemented by bean then container sets the enclosing application context itselft in the created bean using the setApplicationContext method
6) If BeanPostProcessor interface is implemented by bean then container calls the postProcessBeforeInitialization() method.
7) If InitializingBean interface is implemented bt the bean then container invokes its afterPropertiesSet() method. If the bean has the init method then that method will be called.
8) If BeanPostProcessor implemented by the bean then container calls its postProcessAfterInitialization() method.
9) Now here the bean is ready to use, It will be in the application context, Till the application context is not destroyed.
10) If DisposableBean interface is implemented by the bean then, Spring container calls its destroy() method. If the bean has the destroy method then the specified method will be called.