Spring initMethod and destroyMethod Example
The initMethod and destroyMethod are attributes of Spring @Bean annotation to perform certain actions upon bean initialization and destruction.
initMethod is called after bean initialization and the destroyMethod is called 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.
initMethod and destroyMethod are alternatives to InitializingBean and DisposableBean.
Related topic,
Related topic,
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-initmethod-destroymethod-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
package com.knf.dev.demo;
public class UserService {
public void init() {
System.out.println("Perform initialization task.");
}
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.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean(initMethod = "init", destroyMethod = "destroy")
public UserService userService(){
return new UserService();
}
}
Here our configuration class is annotated with @Configuration, it is treated as a source of bean definitions, and any methods within the class that are annotated with @Bean returns a bean to be managed by Spring context.
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);
UserService myComponent = context.getBean(UserService.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!