Member-only story
Understanding the Lifecycle of a Bean in Spring Boot

When it comes to developing enterprise-level applications, the Spring Framework — particularly Spring Boot — has become a go-to choice for Java developers. Spring Boot simplifies the process of creating stand-alone, production-grade Spring-based applications that you can “just run.” A fundamental concept in the Spring framework is the bean — a core component that is managed by the Spring IoC (Inversion of Control) container. In this blog post, we will explore the lifecycle of a bean in Spring Boot to better understand how beans are created, managed, and destroyed within the Spring context.
What is a Spring Boot Bean?
Before diving into the lifecycle of a Spring Boot bean, it’s important to define what a bean is. In Spring, a bean is an object that is instantiated, assembled, and managed by the Spring IoC container. These objects form the backbone of your application. They can be a service, a repository, a controller, or any other object that performs a specific role within your application.
The Lifecycle of a Bean
The lifecycle of a Spring Boot bean consists of several phases from its instantiation until it’s destroyed. Spring allows you to hook into its lifecycle through callbacks and annotations. Let’s walk through the key stages of a Spring Boot bean’s life:
1. Instantiation
The first step is the instantiation of a bean. This is where the Spring container creates an instance of the bean. By default, Spring creates beans as singletons, which means each bean is instantiated once per container.
@Component
public class MyBean {
// class body
}
In the example above, when the Spring context is loaded, it will create an instance of MyBean
.
2. Populating Properties
After the instantiation of the bean, Spring will inject any required dependencies and set the properties. This is usually done via constructor injection, setter injection, or field injection.
@Component
public class MyBean {
private final MyDependency myDependency;
@Autowired
public MyBean(MyDependency myDependency) {
this.myDependency =…