Spring Lifecycle InitializingBean and DisposableBean

The InitializingBean and DisposableBean can be used to interact with the spring container’s management lifecycle. The container calls the afterPropertiesSet() and the destroy() methods respectively to perform some actions upon initialization and destruction of spring singleton beans.

  1. If a bean implements the InitializingBean interface, the afterPropertiesSet() method is called after all the properties are set by the spring container.
  2. If a bean implements the DisposableBean interface, the destroy() method is called upon destruction of your singleton bean.

InitializingBean and DisposableBean

The BookStore bean implements the two interfaces: InitializingBean and DisposableBean. Which requires us to implement the afterPropertiesSet() and destroy() methods respectively. These interfaces tightly couple our code with spring. And in most cases isn’t desirable. Better is to configure these methods externally using init-method and destroy-method.

package com.memorynotfound.spring.core.lifecycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class BookStore implements InitializingBean, DisposableBean {

    private String name;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("- - - initializing BookStore bean using initializingBean");
        System.out.println("name: " + name);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("- - - destroying BookStore bean using DisposableBean");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Spring Configuration

We register our bean in the spring configuration and add the properties to the bean.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.memorynotfound.spring.core.lifecycle.BookStore">
        <property name="name" value="Brand New Bookstore"/>
    </bean>

</beans>

Running the Application

The registerShutdownHook registers a shutdown hook that will be called before the JVM is closed. By calling this method, the spring container will release all the resources and destroy all cached singleton beans when the application is shutdown.

package com.memorynotfound.spring.core.lifecycle;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Run {

    public static void main(String... args) throws InterruptedException {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
        context.registerShutdownHook();
    }
}

Output

The initializing method is called after the properties are set by the spring container. When the application is shutdown the destruction method is called.

- - - initializing BookStore bean using initializingBean
name: Brand New Bookstore
- - - destroying BookStore bean using DisposableBean

References

Download

You may also like...