Spring DisposableBean Example

By Arvind Rai, October 28, 2021
On this page we will provide Spring DisposableBean example. The DisposableBean interface is implemented by beans that want to release resources on destruction. The DisposableBean interface contains destroy() method that needs to be defined by our bean. The DisposableBean is used to dispose the cached instances of singleton bean or perform any destruction task. When the Spring application context is about to close, BeanFactory invokes the bean’s destroy() method.

Note: The alternative of DisposableBean is @PreDestroy annotation.

Maven File

Find the Maven file used in our example.
pom.xml
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.5.5</version>
	<relativePath />
</parent>
<properties>
	<java.version>16</java.version>
</properties>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
</dependencies> 

Implement DisposableBean

The DisposableBean has following method.
void destroy() throws Exception 
The BeanFactory invokes the above method on destruction of a bean.

Find the code to use DisposableBean.
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;
	}
        @Override
	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> 

Run Application

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. 

Reference

Interface DisposableBean

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us