Spring InitializingBean and DisposableBean

Concept Overview

Sometimes, after Spring beans are created, they need to perform some additional initialization operations so that they can carry out their responsibilities. e.g. When an ATM (Automated Teller Machine) system is started, it needs to connect to its bank’s network.

Similary, sometimes Spring beans need to peform some cleanup operations before it gets destroyed. e.g.when an ATM system is closed it needs to disconnect from its bank network.

Additional initialization can be done by implementing the
InitializingBean
interface and overriding the
afterPropertiesSet()
method.

Similarly, cleanup can be done by implementing the
DisposableBean
interface and overriding the
destroy()
method.

Note: This approach is an alternative to using
init-method
and
destroy-method
attributes of
bean
tag. See
this trail
for further details.

The following sample program demonstrates the usage of
InitializingBean
and
DisposableBean
interface.

Sample Program Overview

In the sample program, we will create an ATM class which implements
InitialingBean
and
DisposableBean
interfaces.

We will override the afterPropertiesSet() and destroy() methods to perform connect and disconnect operations with bank’s network respectively.

We will configure the ATM bean in spring-config.xml.

Finally, we will test our setup using TestInitializingBeanDisposableBean class which will load and unload Spring context to verify that initialization and cleanup operations happen successfully.

Required Libraries
  • commons-logging.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar

Source Code

Create the ATM class (see code snippet below)

Implement the
InitializingBean
and
DisposableBean
interfaces (see line 6 below)

Override the afterPropertiesSet() and destroy() methods (see lines 9 and 15 below)

The afterPropertiesSet() is called by Spring to perform initialization while the destroy() is caleld by Spring to perform clean up operations.

Declare the ATM class in spring-config.xml (see line 10 below).


Finally, we need a java program to test the our setup.This is done by TestInitializingBeanDisposableBean.java (see source code below)

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans(see line 11 below). This step will load the ATM bean within Spring and shall call the afterPropertiesSet() method to perform initialization.

We call context.close() method to unload Spring beans (see line 13 below). This step will unload the ATM bean within Spring and shall call the destroy() method to perform clean up operations.


Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save the springinitializatingbeandisposablebean-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springinitializatingbeandisposablebean-installer.jar and execute the jar using
    java -jar springinitializatingbeandisposablebean-installer.jar
    command)

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    springinitializatingbeandisposablebean
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    Leave a Comment