Bean Life Cycle

Share:

In this chapter, we will dive into the core component of the Spring Framework which is the Spring Boot Bean Life Cycle. Understanding the life cycle, how it is managed by the Spring Container, and how we can intervene to control and tap into the process is imperative when developing applications using Spring or Spring Boot.

Spring Boot Bean Life Cycle Overview

Spring Boot follows the Singleton design pattern for its Beans. They are created and wired automatically by Spring's IoC (Inversion of Control) container and are ready to use for the entire application.

The Spring Boot Bean Life Cycle is composed of various stages:

  1. Instantiation: The bean is created.
  2. Populating properties: The properties of the bean are set.
  3. BeanNameAware interface: The bean name is set by the container.
  4. BeanFactoryAware interface: Provides the bean a reference of the BeanFactory which it has been created with.
  5. ApplicationContextAware interface: This gives the bean a reference to the ApplicationContext.
  6. Pre-initialization – BeanPostProcessors: Any BeanPostProcessors methods are called.
  7. Initialization – afterPropertiesSet and custom init methods: These specific initialization methods are executed.
  8. Post-initialization- BeanPostProcessors: Any BeanPostProcessors methods are applied after the initialization methods.

Let's go through these stages in detail and learn how to intervene in each stage to customize or audit our beans.

Custom Bean Initialization

Custom Bean Initialization is one part of the initialization process of a bean. We can define custom initialization behavior for beans by implementing the InitializingBean interface. The afterPropertiesSet() method of this interface will be called by the Spring container and allows us to perform any specific initialization task for our bean:

import org.springframework.beans.factory.InitializingBean;

public class Student implements InitializingBean {
    private String name;

    //Getters and Setters

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Initializing bean with interface..");
    }
}

Custom Bean Destruction

Just like how we can define a custom initialization method, we can define methods for bean destruction as well using the DisposableBean interface. Spring will call destroy() method when the context is being closed:

import org.springframework.beans.factory.DisposableBean;

public class Student implements DisposableBean {
    private String name;

    //Getters and Setters

    @Override
    public void destroy() throws Exception {
        System.out.println("Bean has been destroyed..");
    }
}

BeanPostProcessors

BeanPostProcessors is a powerful mechanism to intervene in the lifecycle of beans. It allows us to process our beans before and after the initialization method. The BeanPostProcessor interface defines two life cycle hooks: postProcessBeforeInitialization() and postProcessAfterInitialization() which are called respectively before and after the initialization process:

import org.springframework.beans.factory.config.BeanPostProcessor;

public class CustomBeanPostProcessor implements BeanPostProcessor {
  
   public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      System.out.println("Before Initialization : " + beanName);
      return bean;
   }

   public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      System.out.println("After Initialization : " + beanName);
      return bean;
   }
}

BeanPostProcessors are particularly useful for checking the state of your beans or modify them before they are ready to use.

Understanding the spring boot bean lifecycle and working with it is crucial for every developer. This understanding allows the developer to manage resources effectively and ensure the efficient operation of their applications.

0 Comment


Sign up or Log in to leave a comment


Recent job openings