Spring InitializingBean and DisposableBean - Example
In Spring, the InitializingBean and DisposableBean interfaces can be used to interact with the spring container’s management lifecycle. The container calls the afterPropertiesSet() method from the InitializingBean and the destroy() method from the DisposableBean respectively to perform certain actions upon bean initialization and destruction.
Annotations @PostConstruct and @PreDestroy, standardized by JSR-250, are generally considered the best practice for obtaining lifecycle callbacks in a modern Spring application. Utilizing these annotations designates that our beans are not bound to Spring-concrete interfaces. Using @PostConstruct and @PreDestroy in Spring - click here
If you don't optate to utilize JSR-250, consider the bean definition metadata init-method and destroy-method.
Annotations @PostConstruct and @PreDestroy, standardized by JSR-250, are generally considered the best practice for obtaining lifecycle callbacks in a modern Spring application. Utilizing these annotations designates that our beans are not bound to Spring-concrete interfaces. Using @PostConstruct and @PreDestroy in Spring - click here
If you don't optate to utilize JSR-250, consider the bean definition metadata init-method and destroy-method.
Complete Example
We are creating a simple maven project. You could clone the code from our GitHub repo.
Final Project Directory
Complete pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-initializingbean-disposablebean-example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.0</version>
</dependency>
</dependencies>
</project>
Create Component class
package com.knf.dev.demo;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class MyComponent implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Perform initialization task.");
}
@Override
public void destroy() {
System.out.println("Perform destructive task or release resources.");
}
public void printMessage() {
System.out.println("Hello");
}
}
Create Config
package com.knf.dev.demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class Config {
}
Spring provides a mechanism to identify application component explicitly through the @ComponentScan annotation. We are not passing arguments to @ComponentScan, letting Spring to scan the current package and all of its sub-packages.
Create main class - Application.java
package com.knf.dev.demo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
public class Application {
public static void main(String[] args) {
AbstractApplicationContext context =
new AnnotationConfigApplicationContext(Config.class);
MyComponent myComponent = context.getBean(MyComponent.class);
myComponent.printMessage();
context.close();
}
}
The AbstractApplicationContext.close() method will close the application context, destroying all cached singleton beans and perform finalization like calling the destroy methods.
Console Output:
Perform initialization task.
Hello
Perform destructive task or release resources.
Perform initialization task.
Hello
Perform destructive task or release resources.
Source code
Are you preparing for Spring certification or preparing for Spring interview? Then below examples are interesting for you!