Home  >  Spring Core

Example of DisposableBean in Spring

By Arvind Rai, April 17, 2013
On this page we will provide example of DisposableBean in Spring. DisposableBean is an interface that is implemented by the spring bean that wants to release resources or any destructive task. We need to implement destroy() method of DisposableBean interface in our bean. It is used to dispose the cached instances of singleton bean. When the spring application context is about to close, destroy() method is called. The alternative of DisposableBean is using @PreDestroy annotation.

Gradle File

Find the gradle file to resolve JAR dependencies.
build.gradle
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'
}  



Implement DisposableBean and configure in XML

Book.java
package com.concretepage;
import org.springframework.beans.factory.DisposableBean;
public class Book implements  DisposableBean{
	private String bookName;
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public void destroy() {
	      System.out.println("Perform destructive work or release resources.");
	}
} 

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">
      <property name="bookName" value="Mahabharat"/>
    </bean>
</beans> 

Main Class

To test the application we will use AbstractApplicationContext.registerShutdownHook() that will close the spring application context.
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.
Book Name:Mahabharat
20:33:38.333 [Thread-0] INFO  o.s.c.s.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6a38e57f: startup date [Tue Dec 29 20:33:38 IST 2015]; root of context hierarchy
20:33:38.333 [Thread-0] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
20:33:38.333 [Thread-0] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@589838eb: defining beans [book]; root of factory hierarchy
20:33:38.334 [Thread-0] DEBUG o.s.b.f.s.DisposableBeanAdapter - Invoking destroy() on bean with name 'book'
Perform destructive work or release resources. 
Find the link that will describe about InitializingBean.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us