Bean lifecycle and CallBack Methods in Spring

Following is sequence of a bean lifecycle in Spring:

  1. Instantiate– First the spring container finds the bean’s definition from the XML file and instantiates the bean..
  2. Populate properties– Using the dependency injection, spring populates all of the properties as specified in the bean definition..
  3. Set Bean Name– If the bean implements BeanNameAware interface, spring passes the bean’s id to setBeanName() method.
  4. Set Bean factory– If Bean implements BeanFactoryAware interface, spring passes the beanfactory to setBeanFactory() method.
  5. Pre Initialization– Also called postprocess of bean. If there are any bean BeanPostProcessors associated with the bean, Spring calls postProcesserBeforeInitialization() method.
  6. Initialize beans– If the bean implements IntializingBean,its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called.
  7. Post Initialization– If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
  8. Ready to use– Now the bean is ready to use by the application.
  9. Destroy– If the bean implements DisposableBean , it will call the destroy() method .

Spring lifetime callbacks

The Spring Framework provides several callback interfaces to change the behavior of your bean in the container; they include InitializingBean and DisposableBean.

The life cycle of a Spring bean is easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required.

Initialization callbacks:

Implementing the org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies exactly one method:

void afterPropertiesSet() throws Exception;

Generally, the use of the InitializingBean interface can be avoided and is actually discouraged since it unnecessarily couples the code to Spring.You have to use afterPropertiesSet(),you can not change name of method.There is alternative for this i.e. XML-based configuration metadata.This is done using the ‘init-method’ attribute of <bean> tag.It provides flexibility of changing method name.

<bean id="countryBean" class="com.technicalstack.Country" init-method="init"/>

public class Country{

public void init() {
// do some initialization work
}
}
…is exactly the same as…

<bean id="countryBean" class="com.technicalstack.Country"/>

public class Country implements InitializingBean {

public void afterPropertiesSet() {
// do some initialization work
}
}
… but does not couple the code to Spring.

Destruction callbacks:

Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a single method:

void destroy() throws Exception;

Generally, the use of the DisposableBean interface can be avoided and is actually discouraged since it unnecessarily couples the code to Spring.You have to use destroy(),you can not change name of method.There is alternative for this i.e. XML-based configuration metadata.This is done using the ‘destroy-method’ attribute of <bean> tag.It provides flexibility of changing method name.

<bean id=”countryBean” class=”com.technicalstack.Country” init-method=”destroy”/>

public class Country{

public void destroy() {
// do some destruction work(like releasing pooled connections)
}
}
…is exactly the same as…

<bean id="countryBean" class="com.technicalstack.Country"/>

public class Country implements DisposableBean{

public void destroy() {
// do some destruction work(like releasing pooled connections)
}
}
… but does not couple the code to Spring.

====================================================================
Default Initialization and destroy method:

If you have too many beans having initialization and or destroy methods with the same name, you don’t need to declare init-method and destroy-method on each individual bean. Instead framework provides the flexibility to configure such situation using default-init-method and default-destroy-method attributes on the <beans> element as follows:

view plainprint?
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”
default-init-method=”init”
default-destroy-method=”destroy”
>

<bean id=”country” class=”com.technicalstack.Country”>
<property name=”countryName” value=”India”/>
</bean>

</beans>


   

Comments are closed