Bean Life Cycle callback methods - interface InitializingBean, DisposableBean

To interact with the container's management of the bean lifecycle, you can implement the Spring InitializingBean and DisposableBean interfaces. The container calls afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction of your beans.

Please follow the below steps :
  • Create a POJO class : Employee.java
  • Create Spring configuration file : src\beans.xml
  • Create EmployeeClient.java to create Container
  • Add Spring library in project classpath
  • Verify the directory structure and Run EmployeeClient.java

Create POJO class Employee1.java by implementing InitializingBean and DisposableBean interface
package techmyguru.spring;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Employee1 implements InitializingBean, DisposableBean {
private String eid, name, city;
private double salary;

@Override
public void afterPropertiesSet() throws Exception
{
System.out.println("it is afterPropertiesSet method");
}

@Override
public void destroy() throws Exception
{
  System.out.println("It is distroy method");
  eid = "";
  name = "";
  city = "";
  salary = 0.0d;
}
// all getter and setter methods
@Override
public String toString()
{
return eid + "\t" + name + "\t" + city + "\t" + salary;
}
}

Create Spring configuration file : src\beans.xml
<?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-3.2.xsd">
  <bean id="emp1" class="techmyguru.spring.Employee1">
    <property name="eid" value="A001" />
    <property name="name" value="Krishna Kumar" />
    <property name="city" value="Chennai" />
    <property name="salary" value="999999" />
  </bean>
</beans>

Create EmployeeClient.java to create Container
EmployeeClient.java
package techmyguru.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class EmployeeClient
{
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Employee1 emp1 = (Employee1ctx.getBean("emp1");
System.out.println(emp1);
try{
emp1.destroy();
catch (Exception e){
e.printStackTrace();
}
}
}

Add Spring library in project classpath : Add the following jar files.
commons-logging-1.0.4.jar
spring-tx-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-test-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar

Verify the directory structure :


Run the EmployeeClient.java and get output :


Previous Download Source Code Next

Comment by : Nabib October 3rd, 2015
Comment : Wow, your post makes mine look felbee. More power to you!


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code