Spring init-method and destroy-method Example in XML Configuration

By Arvind Rai, March 12, 2023
On this page, we will provide Spring bean init-method and destroy-method example in XML configuration. The init-method and destroy-method are properties of Spring <bean> element. The init-method declares the custom method name of bean class which will act as initialization method. The destroy-method 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

The 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.
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







©2024 concretepage.com | Privacy Policy | Contact Us