Friday, 27 June 2014

Bean Lifecycle in Spring Framework

            The Spring Framework is based on the Inversion of Control (IoC) principle, which is why it is often referred to as an IoC container. Spring beans reside within the IoC container and are managed by it. A Spring bean is simply a Plain Old Java Object (POJO) that is instantiated, configured, and managed by the Spring container.

The following steps explain the lifecycle of a Spring bean within the container.
  1. The container looks for the bean definition in the configuration file (e.g., beans.xml).
  2. Using the Reflection API, the container creates the bean object. If any properties are defined in the bean definition, the container also injects and initializes those properties.
  3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
  4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
  5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called before the properties for the Bean are set.
  6. If an init() method is specified for the bean, it will be called.
  7. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
  8. If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.

No comments:

Post a Comment