How to use InitializingBean and DisposableBean interface for initializing and destroying a bean in Spring Framework ?.

Program to demonstrate how to use InitializingBean and DisposableBean interface for initializing and destroying a bean in Spring Framework

Step 1 - Create a POJO class here its Employee.java having getters, setters, constructor and implementing two interfaces InitializingBean and DisposableBean, along with its unimplemented methods as afterPropertiesSet() and destroy()

package com.hubberspot.spring.InitDestroy;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// ApplicationContext is a Spring interface which
// provides with the configuration for an application.
// It provides us with all the methods that BeanFactory
// provides. It loads the file resources in a older
// and generic manner. It helps us to publish events to the
// listener registered to it. It also provides quick support
// for internationalization. It provides us with the object
// requested, it reads the configuration file and provides
// us with the necessary object required.
// We are using concrete implementation of ApplicationContext
// here called as ClassPathXmlApplicationContext because this
// bean factory reads the xml file placed in the classpath of
// our application. We provide ClassPathXmlApplicationContext
// with a configuration file called as spring.xml placed
// at classpath of our application.
AbstractApplicationContext context =
new ClassPathXmlApplicationContext(("spring.xml"));
// Using AbstractApplicationContext registerShutdownHook()
// method to demonstrate destroy() method being called
context.registerShutdownHook();
// In order to get a object instantiated for a particular bean
// we call getBean() method of ClassPathXmlApplicationContext
// passing it the id for which the object is to be needed.
// Here getBean() returns an Object. We need to cast it back
// to the Employee object. Without implementing new keyword we
// have injected object of Employee just by reading an xml
// configuration file.
Employee employee = (Employee)context.getBean("employee");
System.out.println("The name of Employee is : " + employee.getName());
System.out.println("The age of Employee is : " + employee.getAge());
}
}

Output of the program :

Program to demonstrate how to use InitializingBean and DisposableBean interface for initializing and destroying a bean in Spring Framework

Step 1 - Create a POJO class here its Employee.java having getters, setters, constructor and implementing two interfaces InitializingBean and DisposableBean, along with its unimplemented methods as afterPropertiesSet() and destroy()

package com.hubberspot.spring.InitDestroy;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// ApplicationContext is a Spring interface which
// provides with the configuration for an application.
// It provides us with all the methods that BeanFactory
// provides. It loads the file resources in a older
// and generic manner. It helps us to publish events to the
// listener registered to it. It also provides quick support
// for internationalization. It provides us with the object
// requested, it reads the configuration file and provides
// us with the necessary object required.
// We are using concrete implementation of ApplicationContext
// here called as ClassPathXmlApplicationContext because this
// bean factory reads the xml file placed in the classpath of
// our application. We provide ClassPathXmlApplicationContext
// with a configuration file called as spring.xml placed
// at classpath of our application.
AbstractApplicationContext context =
new ClassPathXmlApplicationContext(("spring.xml"));
// Using AbstractApplicationContext registerShutdownHook()
// method to demonstrate destroy() method being called
context.registerShutdownHook();
// In order to get a object instantiated for a particular bean
// we call getBean() method of ClassPathXmlApplicationContext
// passing it the id for which the object is to be needed.
// Here getBean() returns an Object. We need to cast it back
// to the Employee object. Without implementing new keyword we
// have injected object of Employee just by reading an xml
// configuration file.
Employee employee = (Employee)context.getBean("employee");
System.out.println("The name of Employee is : " + employee.getName());
System.out.println("The age of Employee is : " + employee.getAge());
}
}