0
0
SpringbootConceptBeginner · 3 min read

Bean Lifecycle in Spring Boot: How It Works and When to Use

The bean lifecycle in Spring Boot is the process where Spring creates, initializes, manages, and destroys beans (objects) in the application context. Spring controls this lifecycle by calling specific methods and hooks at different stages, allowing developers to customize behavior during bean creation and destruction.
⚙️

How It Works

Think of a bean in Spring Boot like a plant in a garden. Spring acts as the gardener who plants the seed (creates the bean), waters it (initializes it), watches it grow (manages it), and finally removes it when it's no longer needed (destroys it). This process is automatic and helps keep your application organized and efficient.

When your application starts, Spring scans for beans and creates them. It then calls special methods to prepare the beans for use, such as setting properties or running custom setup code. When the application stops, Spring cleans up by calling methods to release resources or perform cleanup tasks.

This lifecycle is controlled by interfaces and annotations like InitializingBean, DisposableBean, @PostConstruct, and @PreDestroy, which let you add your own code at key points.

💻

Example

This example shows a simple Spring Boot bean that prints messages during its lifecycle phases using annotations.

java
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

    public MyBean() {
        System.out.println("Constructor: Bean is being created");
    }

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

    @PreDestroy
    public void cleanup() {
        System.out.println("@PreDestroy: Bean is about to be destroyed");
    }
}
Output
Constructor: Bean is being created @PostConstruct: Bean is initialized @PreDestroy: Bean is about to be destroyed
🎯

When to Use

Use the bean lifecycle in Spring Boot when you need to run code at specific times during a bean's life. For example, you might want to open a database connection when the bean starts and close it when the bean is destroyed. Or you might want to load configuration or initialize resources only once.

This is helpful in real-world apps for managing resources efficiently, avoiding memory leaks, and ensuring your beans are ready to use when needed.

Key Points

  • Spring manages bean creation, initialization, and destruction automatically.
  • You can customize lifecycle behavior using @PostConstruct and @PreDestroy annotations or lifecycle interfaces.
  • Proper lifecycle management helps resource handling and application stability.
  • Beans are created when the application context starts and destroyed when it closes.

Key Takeaways

Spring Boot controls bean lifecycle to manage creation, initialization, and destruction automatically.
Use @PostConstruct and @PreDestroy to add custom setup and cleanup code in beans.
Lifecycle management helps efficiently handle resources like database connections or files.
Beans live as long as the application context is active and are destroyed when it shuts down.