Back to Spring Boot Course

Spring Boot Course

In Spring Boot (and Spring Framework in general), the bean life cycle refers to the process through which a Spring bean is created, configured, and eventually destroyed. This life cycle is managed by the Spring container, which takes care of instantiating, initializing, and disposing of beans according to certain rules.

Steps in the Bean Life Cycle

  1. Bean Creation

When the Spring context is initialized (e.g., when the application starts), Spring scans for beans defined in configuration files, annotations (@Component, @Service, etc.), or Java Config (@Configuration).

The beans are then instantiated based on the configuration (e.g., using annotations or XML).

  1. Dependency Injection

After a bean is instantiated, Spring performs dependency injection. It injects any necessary dependencies into the bean, either through constructor injection, setter injection, or field injection.

  1. Post-Processors

BeanPostProcessors are used to perform operations on beans before and after their initialization methods are called.

  • Before initialization: A BeanPostProcessor's postProcessBeforeInitialization method is called.
  • After initialization: The postProcessAfterInitialization method is invoked.
  1. Initialization

If the bean implements the InitializingBean interface, its afterPropertiesSet() method is called.

If a custom initialization method is defined (using the @PostConstruct annotation or init-method attribute in XML configuration), that method is called at this point.

  1. Bean Usage

Once the bean is fully initialized, it can be used by the Spring container or other beans as needed.

  1. Destruction

When the Spring context is destroyed (e.g., at application shutdown), the beans are cleaned up.

If the bean implements the DisposableBean interface, its destroy() method is called.

If a custom destroy method is defined (using the @PreDestroy annotation or destroy-method in XML configuration), that method is called at this point.

  1. Post-Destruction

Any post-destruction steps are performed by the BeanPostProcessors after the destroy() methods are invoked.

Let's see a simple example of the lifecycle of a Spring bean in a Spring Boot application:

import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Component public class MyBean implements InitializingBean, DisposableBean { public MyBean() { System.out.println("Bean Constructor: MyBean instance is created"); } @PostConstruct public void init() { System.out.println("PostConstruct: Initialization method is called"); } @Override public void afterPropertiesSet() { System.out.println("InitializingBean: afterPropertiesSet method is called"); } public void customInit() { System.out.println("Custom Init: custom initialization method"); } @PreDestroy public void preDestroy() { System.out.println("PreDestroy: Pre-destroy method is called"); } @Override public void destroy() { System.out.println("DisposableBean: destroy method is called"); } public void customDestroy() { System.out.println("Custom Destroy: custom destroy method"); } }
  • Bean Constructor: The MyBean constructor is called when the bean is created.
  • @PostConstruct: This method is invoked after the bean's properties are set (but before the afterPropertiesSet() method).
  • InitializingBean Interface: The afterPropertiesSet() method is called after @PostConstruct and any other initialization-related methods.
  • customInit(): If we specify a custom init method, it can be invoked here.
  • @PreDestroy: This method is called before the bean is destroyed.
  • DisposableBean Interface: The destroy() method is invoked when the bean is about to be destroyed.
  • customDestroy(): Custom destroy method can also be specified.

Bean Life Cycle Summary

screenshot of the app

  1. Instantiation: Spring creates the bean.
  2. Dependency Injection: Spring injects the bean's dependencies.
  3. Bean Post Processors (Before Initialization): BeanPostProcessors are called before the initialization.
  4. Initialization: Initialization happens, including @PostConstruct or InitializingBean methods.
  5. Bean Post Processors (After Initialization): BeanPostProcessors are called after initialization.
  6. Destruction: When the application shuts down or the context is closed, destruction methods are called (@PreDestroy, DisposableBean, etc.).