How to implement Lifecycle callbacks in Spring beans?

In this article we will discuss about the Lifecycle callbacks methods available in Spring Container. Now to start the discussion we need to know “What is Lifecycle callbacks for Spring beans?”

Lifecycle callbacks are the ways by which beans can interact with the container management in different phases of the lifecycle of that bean.

As an example: 

When a bean is instantiated, it may require to perform some initialization operation to get into the usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup operation may be required. Spring provides 2 interfaces as InitializingBean and DisposableBean to handle such scenarios. The InitializingBean has a method afterPropertiesSet() which will be get called automatically by the container once the bean is instantiated, if our bean is implementing the InitializingBean  interface. The DisposableBean interface has a destroy()  method which will called when the bean is removed from the spring context.

Internally, the Spring Framework uses BeanPostProcessor implementations to process any callback interfaces it can find and call the appropriate methods.

Initialization callbacks : The 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 a single method:

void afterPropertiesSet() throws Exception;

Again there are 2 (Except The JSR-250 @PostConstruct ) different approaches to invoke the initialization callback in Spring. We can implement the InitializingBean interface and override the afterPropertiesSet() method or we can do the same by defining the init-method attribute in xml configuration. Following is the example:

<bean id="testBean" class="examples.TestBean" init-method="init"/>

public class TestBean {

    public void init() {
        // do some initialization work
    }
}

…is exactly the same as…

< bean id="testBean" class="examples.TestBean" />

public class TestBean implements InitializingBean {

    public void afterPropertiesSet() {
        // do some initialization work
    }
}

Destruction callbacks : We need to implement the DisposableBean interface to get a callback when the container is destroying. The DisposableBean interface specifies a single method:

void destroy() throws Exception;

Following are the way to implement the destruction callbacks:

<bean id="testBean" class="examples.TestBean" destroy-method="cleanup"/>

public class TestBean {

    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }

}

…is exactly the same as…

<bean id=" testBean" class="examples.TestBean"/>

public class TestBean implements DisposableBean {

    public void destroy() {
        // do some destruction work (like releasing pooled connections)
    }

}



 

spring 12 constructor 12

FOLLOW US ON LinkedIn



Explore Tutu'rself