Life Cycle of a Bean in Spring
A Spring Bean is managed by IoC Container / Spring Container, whenever a bean is declared in the Spring configuration file, container will find it and do the following steps to make available for the application.
1) Firstly, spring creates an instance of the Bean using Java Reflection API.
2) If there are any properties associated with bean then spring injects those properties. If there are any bean as a peoperty then spring finds that bean and inject it.
3) If the Bean class implements the BeanNameAware interface, then the spring will call setBeanName() method by passing the name of the Bean.
4) If the Bean class implements the BeanFactoryAware interface, then the spring will call setBeanFactory() method by passing an instance of BeanFactory object.
5) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessBeforeInitialization() method before the properties for the Bean are injected.
6) If the Bean class implements the InitializingBean interface, then the spring will call afterPropertiesSet() method once all the Bean properties defined in the Configuration file are injected.
7) If there is any custom init-method declared in the configuration file, that method will be called.
8) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessAfterInitialization() method.
9) Now bean is ready to use.
10) If the Bean class implements the DisposableBean interface, then the spring calls destroy() method when the Application no longer needs the bean reference.
11) If there is any custom destroy-method declared in the configuration file, that method will be called.
1) Firstly, spring creates an instance of the Bean using Java Reflection API.
2) If there are any properties associated with bean then spring injects those properties. If there are any bean as a peoperty then spring finds that bean and inject it.
3) If the Bean class implements the BeanNameAware interface, then the spring will call setBeanName() method by passing the name of the Bean.
4) If the Bean class implements the BeanFactoryAware interface, then the spring will call setBeanFactory() method by passing an instance of BeanFactory object.
5) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessBeforeInitialization() method before the properties for the Bean are injected.
6) If the Bean class implements the InitializingBean interface, then the spring will call afterPropertiesSet() method once all the Bean properties defined in the Configuration file are injected.
7) If there is any custom init-method declared in the configuration file, that method will be called.
8) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessAfterInitialization() method.
9) Now bean is ready to use.
10) If the Bean class implements the DisposableBean interface, then the spring calls destroy() method when the Application no longer needs the bean reference.
11) If there is any custom destroy-method declared in the configuration file, that method will be called.