0
0
Spring Bootframework~5 mins

Bean lifecycle overview in Spring Boot

Choose your learning style9 modes available
Introduction

Beans are objects managed by Spring. Understanding their lifecycle helps you control how and when they are created, used, and destroyed.

When you want to customize how your objects start and stop in a Spring app
When you need to run some code right after a bean is created or before it is destroyed
When you want to manage resources like database connections safely
When you want to understand how Spring manages your app components behind the scenes
Syntax
Spring Boot
public class MyBean {

    // Initialization method
    public void init() {
        // code to run after bean creation
    }

    // Destruction method
    public void destroy() {
        // code to run before bean destruction
    }
}

The init method runs after the bean is created and dependencies are set.

The destroy method runs before the bean is removed from the container.

Examples
Using @PostConstruct and @PreDestroy annotations to mark init and destroy methods.
Spring Boot
@Component
public class MyBean {

    @PostConstruct
    public void init() {
        System.out.println("Bean is initialized");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Bean is being destroyed");
    }
}
Implementing Spring interfaces to handle lifecycle callbacks.
Spring Boot
@Component
public class MyBean implements InitializingBean, DisposableBean {

    @Override
    public void afterPropertiesSet() {
        System.out.println("Bean is initialized");
    }

    @Override
    public void destroy() {
        System.out.println("Bean is being destroyed");
    }
}
Specifying init and destroy methods directly in the @Bean annotation.
Spring Boot
@Bean(initMethod = "init", destroyMethod = "cleanup")
public MyBean myBean() {
    return new MyBean();
}

public class MyBean {
    public void init() {
        System.out.println("Bean initialized");
    }
    public void cleanup() {
        System.out.println("Bean destroyed");
    }
}
Sample Program

This Spring component prints messages when it is created and before it is destroyed, showing the bean lifecycle in action.

Spring Boot
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;

@Component
public class SimpleBean {

    @PostConstruct
    public void init() {
        System.out.println("SimpleBean initialized");
    }

    @PreDestroy
    public void cleanup() {
        System.out.println("SimpleBean destroyed");
    }
}
OutputSuccess
Important Notes

Beans are created when the Spring container starts and destroyed when it shuts down.

Use @PostConstruct and @PreDestroy for simple lifecycle methods.

For prototype-scoped beans, Spring does not manage destruction automatically.

Summary

Spring beans have a lifecycle: creation, initialization, use, and destruction.

You can add code to run at init and destroy phases using annotations or interfaces.

Understanding this helps manage resources and app behavior cleanly.