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">
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"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
Consider DatabaseInitiaizer class which has init() and destroy() methods to manage callbacks.


package net.javaguides.spring;

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

import org.springframework.stereotype.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");

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


    public void destroy() {
        // Delete from database
        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("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) {
        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;

    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;

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);


-----------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

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course