Spring @Bean’s initMethod and destroyMethod Attributes Example

In this article, we will discuss how to use initMethod and destroyMethod attributes of the @Bean annotation to perform certain actions after bean initialization or before bean destruction by a container.
Check out Spring @PostConstruct and @PreDestroy Example
As we use init-method and destroy-method attributes in an XML bean configuration same way we can use initMethod and destroyMethod in @Bean Java-based configuration.
<bean id="customerService" class="net.javaguides.spring.DatabaseInitiaizer" 
 init-method="init" destroy-method="destroy">
</bean>
In this article, we use the latest release of Spring 5.1.0.RELEASE version to demonstrate this example.

Spring @Bean’s initMethod and destroyMethod Attributes Example

In real time projects, we populate a database table with some records during application startup and will delete records from same database table during application shutdown.
In this example, we will populate the in-memory List data structure with few user objects during application startup using init() method. We will also delete user objects from List during application shutdown using destroy() method.

Tools and technologies used

  • Spring Framework - 5.1.0.RELEASE
  • JDK - 8 or later
  • Maven - 3.2+
  • IDE - Eclipse Mars/STS

Create a Simple Maven Project

Create a simple maven project using your favorite IDE and refer below section for packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.

Project Structure

Below diagram shows a project structure for your reference - 

The pom.xml File

<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>net.javaguides.spring</groupId>
 <artifactId>spring-bean-lifecycle</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>5.1.0.RELEASE</version>
  </dependency>
  
 </dependencies>
 <build>
  <sourceDirectory>src/main/java</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>
Consider DatabaseInitiaizer class which has init() and destroy() methods to manage callbacks.

DatabaseInitiaizer.java

package net.javaguides.spring;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class DatabaseInitiaizer {

    private List < User > listOfUsers = new ArrayList < > ();

    public void init() {
        User user = new User(1, "User");
        User user1 = new User(2, "Admin");
        User user2 = new User(3, "SuperAdmin");

        listOfUsers.add(user);
        listOfUsers.add(user1);
        listOfUsers.add(user2);
        System.out.println("-----------List of users added in init() method ------------");
        for (Iterator < User > iterator = listOfUsers.iterator(); iterator.hasNext();) {
            User user3 = (User) iterator.next();
            System.out.println(user3.toString());
        }
        // save to database

    }

    public void destroy() {
        // Delete from database
        listOfUsers.clear();
        System.out.println("-----------After of users removed from List in destroy() method ------------");
        for (Iterator < User > iterator = listOfUsers.iterator(); iterator.hasNext();) {
            User user3 = (User) iterator.next();
            System.out.println(user3.toString());
        }
        System.out.println("End of destroy()  method");
    }
}

Create POJO - User.java

package net.javaguides.spring;

public class User {
    private Integer id;
    private String name;

    public User() {}

    public User(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }
}

Java Based Configuration - AppConfig.java

Create AppConfig class and write the following code in it.
package net.javaguides.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public DatabaseInitiaizer databaseInitiaizer() {
        return new DatabaseInitiaizer();
    }
}

Running Spring Application - Application.java

Let's create a main class and run an application.
package net.javaguides.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();
    }
}

Output

-----------List of users added in init() method ------------
User [id=1, name=User]
User [id=2, name=Admin]
User [id=3, name=SuperAdmin]
-----------After of users removed from List in destroy() method ------------
End of destroy()  method
The source code of this example is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial


Comments