Saturday, April 25, 2015

Spring bean life cycle

  • Spring container find the bean definition from XML and instantiates the bean
  • By using dependency injection, spring populates all properties of bean
  • If bean implements BeanNameAware interface, the factory call setBeanName(), passing then name of bean.
  • If bean implements BeanFactoryAware' interface, the factory calls setBeanFactory() will be called by passing an instance of BeanFactory object.
  •  If there are any BeanPostProcessors object associated with the BeanFactory that loaded the Bean, then the method postProcessBeforeInitialization() will be called even before the properties for the Bean are set
  • If the Bean class implements the InitializingBean interface, then the method afterPropertiesSet() will be called once all the Bean properties defined in the Configuration file are set.
  • If Bean definition contains init-method is specified for the bean, it will be called.
  • The postProcessAfterInitialization() method will be called if there are any Bean Post Processors attached for the Bean Factory object.
  • If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
  • If Bean definition contains a destroy-method attribute, then the corresponding method definition in the Bean class will be called.