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
Similarly, cleanup can be done in a method and its name can be configured within Spring using the
The following sample program demonstrates the usage of
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
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.
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).
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
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
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
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
. 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
tag (see lines 9-10 below)