Home  >  Spring Core

Spring init-method and destroy-method Example using XML

By Arvind Rai, December 30, 2015
On this page we will provide spring init-method and destroy-method example using XML. init-method and destroy-method are the attribute of spring bean tag. init-method declares the custom method name of bean which will act as bean initialization method. destroy-method attribute will define custom method name that will act as destroy method. To use init-method and destroy-method with annotation, find the link

@PostConstruct and @PreDestroy Annotation

init-method

init-method is the attribute of spring <bean> tag. It is used to declare a custom method for the bean which will act as bean initialization method. We can define it as follows.
<bean id="book" class="com.concretepage.Book" init-method="myPostConstruct">
 
Here myPostConstruct() method is the bean initialization method in Book class. This initialization method is called after initializing bean properties. We can use such initialization method to validate the value of bean properties or initializing any task. The InitializingBean interface in spring performs the same task but it is highly coupled to spring, so we should prefer init-method.

destroy-method

destroy-method is bean attribute using which we can assign custom bean method that will be called just before the bean is destroyed. To use destroy-method attribute, we do as follows.
<bean id="book" class="com.concretepage.Book" destroy-method="myPreDestroy">
 
Here myPreDestroy method will be defined in Book class. Spring will call this method just before destroying the bean. destroy-method is used to release resources or perform some destruction task. DisposableBean interface in spring performs the same task but it is highly coupled to spring, so we should prefer destroy-method.

Create Bean

Find the class which is implementing myPostConstruct() as initialization method and myPreDestroy() as destroy method.
Book.java
package com.concretepage;
public class Book {
	private String bookName;
	public void myPostConstruct() {
	     System.out.println("---Do initialization task---");
	     if(bookName != null) {
	    	 System.out.println("bookName property initialized with the value:"+ bookName);
	     }
	}	
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public void myPreDestroy() {
		System.out.println("---Release resources or perform destruction task---");
	}
} 

Spring XML for init-method and destroy-method

Find the XML file in which we have defined our bean with init-method and destroy-method as bean attribute of bean tag.
spring-config.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.xsd">

    <bean id="book" class="com.concretepage.Book" init-method="myPostConstruct"
             destroy-method="myPreDestroy">
      <property name="bookName" value="Mahabharat"/>
    </bean>
</beans> 

build.gradle

Find the gradle file to resolve the JAR dependencies.
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringDemo'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
    compile 'org.springframework:spring-context:4.1.8.RELEASE'
}  

Run Demo

To test the application we are initializing spring container and then shutting down.
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		Book book = (Book)context.getBean("book");
		System.out.println("Book Name:"+ book.getBookName());
	        context.registerShutdownHook();
	}
} 
Find the output.
---Do initialization task---
bookName property initialized with the value:Mahabharat
Book Name:Mahabharat
---Release resources or perform destruction task--- 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us