Saturday, April 26, 2014

Spring Bean Life Cycle


Lets see the life cycle of bean from cradle to grave or in another words from instantiation to destroy method.

Once Spring reads the bean definition xml file

1) Spring instantiates the bean.
2) Spring injects the values and bean references into bean references.
3) If your bean implements the BeanNameAware interface, Spring calls the setBeanName() method.
4) If your bean implements BeanFactoryAware interface then Spring calls the setBeanFactory() method and passing the bean factory itself.
5) If your bean implements ApplicationContextAware interface then Spring calls the setApplicationContext() method.
6) If your bean implements BeanPostProcessor interface then Spring calls postProcessBeforeInitialization() method
7) If your bean implements InitializingBean interface then Spring calls afterPropertiesSet() method and if your bean declared any init-method then the specified init method will be called.
8) If there are any beans that implements BeanPostProcessor then Spring calls postProcessAfterInitialization() method.
9) Now your bean is ready and will be available till the application context is destroyed.
10) If your bean implements DisposableBean interface then Spring will call destroy method similarly if your bean declared any user defined destroy method then Spring calls the destroy-method.

Thanks for visiting my blog....