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 in a method and its name can be configured within Spring using the
init-method
attribute.
Similarly, cleanup can be done in a method and its name can be configured within Spring using the
destroy-method
attribute.
The following sample program demonstrates the usage of
init-method
and
destroy-method
attributes in Spring.
Sample Program Overview
In the sample program, we will create an ATM class with method init() and cleanUp() used to perform connect and disconnect operations with bank’s network respectively.
We will define the
init-method
and
destroy-method
attributes pointing to init() and cleanUp() methods for the ATM bean in spring-config.xml.
Also, we will test our setup using TestInitMethodDestroyMethod class which will load and unload Spring context to verify whether initialization and cleanup operations happen successfully.
- log4j.jar
- org.springframework.core.jar
- org.springframework.context.jar
- org.springframework.context.support.jar
- org.springframework.beans.jar
- commons-logging.jar
- org.springframework.aop.jar
- org.springframework.asm.jar
- org.springframework.expression.jar
Create ATM class
The init() method is used to connect to banks network (see line 5 below).
The cleanUp() method is used to disconnect the ATM from the bank’s network (see line 9 below).
Declare the ATM class in spring-config.xml (see line 10below).
Using Spring’s
init-method
and
destroy-method
attributes to call the init() method and cleanUp() method defined in ATM class (see line 12 below).
Finally, we need a java program to test the Method Injection setup.This is done by TestInitMethodDestroyMethod.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 init() method by using the
init-method
attribute.
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 cleanUp() method by using the
destroy-method
attribute.
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.
(Alternatively you can go the folder containing the springinitmethoddestroymethod-installer.jar and execute the jar using
java -jar springinitmethoddestroymethod-installer.jar
command)
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
springinitmethoddestroymethod
. 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.
If many beans in the spring-context.xml need to perform initialization and clean up activities and all the beans have the same signatures for initilization and destroy methods then it is possible to define them in one place by using the
default-init-method
and
default-destroy-method
in Spring’s
beans
tag (see lines 9-10 below)