Spring init-method and destroy-method - Example

The init-method and destroy-method are attributes of Spring bean configuration file to perform certain actions upon bean initialization and destruction. 

init-method is called after bean initialization and the destroy-method is called during before bean destruction by container.

Annotations @PostConstruct and @PreDestroy, standardized by JSR-250, are generally considered the best practice for obtaining lifecycle callbacks in a modern Spring application. 

init-method and destroy-method are alternatives to InitializingBean and DisposableBean

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-init-method-destroy-method-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 UserService class

UserService consist of init() method, destroy() method, and username field including getter and setter.
package com.knf.dev.demo;

public class UserService {

private String username;

public void init() {
System.out.println("Perform initialization task.");
System.out.println("Username= "+username);
}


public void destroy() {
System.out.println("Perform destructive task or release resources.");
}


public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}


Create XML for Spring init-method and destroy-method

Define init-method and destroy-method as attribute in bean configuration file. 
<?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="userService" class="com.knf.dev.demo.UserService"
init-method="init"
destroy-method="destroy">
<property name="username" value="Scott Morrison"/>
</bean>
</beans>


Create main class - Application.java 

package com.knf.dev.demo;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {

public static void main(String[] args) {

AbstractApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService = (UserService)context.getBean("userService");
System.out.println("User Name= "+ userService.getUsername());

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.

Username= Scott Morrison

User Name= Scott Morrison

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!

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete