Spring init-method and destroy-method example

In this post, we will see about Spring init-method and destroy-method.
When bean is instantiated or destroyed , there are some operations you may need to perform, so you can use init-method and destroy-method to call this methods while bean is being created or destroyed.
Lets understand it with the help of simple example:
For configuring spring in your eclipse ide please refer  hello world example

1.Country.java:

This is simple pojo class which have only one attribute as countryName.
Create Country.java under package org.arpit.java2blog.Copy following content into Country.java.
package org.arpit.java2blog;

public class Country {

 String countryName ;
 
 public String getCountryName() {
  return countryName;
 }
 
 
 public void setCountryName(String countryName) {
  this.countryName = countryName;
 }

 public void init()
 {
  System.out.println("In init block of country");
 }

 public void destroy()
 {
  System.out.println("In destroy block of country");
 }
}

2.SpringInitDestroyMain.java

This class contains main function.Create SpringInitDestroyMain.java under package org.arpit.java2blog.Copy following content into SpringInitDestroyMain.java
package org.arpit.java2blog;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringInitDestroyMain{

 public static void main(String[] args) {

  AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
  Country countryObj = (Country) appContext.getBean("country");
  System.out.println("Country Name: "+countryObj.getCountryName());
  appContext.registerShutdownHook();
 }
}
Here you need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApplicationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.

3.ApplicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="country" class="org.arpit.javapostsforlearning.Country" init-method="init" destroy-method="destroy">
 <property name="countryName" value="India"/>
 </bean>
  
</beans>

4.Run it

When you will run above application,you will get following as output.
In init block of country
Country Name: India
In destroy block of country

Default Initialization and destroy method:

If you want to put default methods for all beans and do not want to configure of individual beans, you can use default-init-method and default-destroy-method  with <beans> tag, so it will call init and destroy methods for all beans configured in ApplicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
 default-init-method="init"
 default-destroy-method="destroy"
 >
 
 <bean id="country" class="org.arpit.javapostsforlearning.Country">
 <property name="countryName" value="India"/>
 </bean>
  
</beans>

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012