Spring Bean Life Cycle Explained [With Coding Example]

Bean is an object in an application. A bean is created, used, and finally destroyed when its purpose is over. These are the different stages of a spring life cycle. The entire spring bean life cycle is supervised by the Spring IoC (Inversion of Control) container. That is why these beans are called spring beans.

source

The Life Cycle of a Spring Bean

source

In a spring bean life cycle, first of all, a bean is instantiated. After instantiation, a bean goes through a sequence of steps before being ready to be used. When a bean is no longer required for any function, it is destroyed.

Read: Top 18 Exciting Spring Projects Ideas & Topics For Beginners

Spring bean life cycle can be controlled in the following ways

  • Instantiation by using:
  • InitializingBean callback interface.
  • Custom init() method from the bean configuration file.
  • Aware interfaces for distinct actions.
  • PostConstruct and PreDestroy annotations.
  • Destruction
  • DisposableBean callback interface
  • Custom destroy() method from the bean configuration file.

Instantiation of a bean

The first process in the spring bean life cycle is an instantiation. The creation of a bean rests on JAVA or XML bean configuration file. This can be done in two ways. 

They are:

  • InitializingBean callback interface: Instantiation in this way is done in a method named afterPropertiesSet(). This method is present in org.springframework.beans.factory.InitializingBean interface. In the program below, a class is created which implements this interface. This enables using the afterPropertiesSet()  method of that interface in the created class.

Following is the program depicting this instantiation process

import org.springframework.beans.factory.InitializingBean;

public class Creatingbean implements InitializingBean

{

@Override

public void afterPropertiesSet() throws Exception

{

// Bean is initialized

}

}

  • Custom created method of instantiation in the bean configuration file: In this process, an XML based configuration file is used. Init-method of this file is used to specifically name the instantiation method. This method is used in the class for bean instantiation. The local definition of a single bean is shown below. In this way, we can create a single bean.

beans.xml:

<beans>

<bean id=”creatingbean” class=”com.workinginjava.work.Creatingbean”

init-method=”createInit” ></bean>

</beans>

Check out: Top 7 Exciting Spring Boot Projects & Topics For Beginners

Following is the program depicting this instantiation process by loading beans.xml

package com.workinginjava.work;

public class Creatingbean 

{

public void createInit()

//Custom created init method is used for instantiation of a bean

}

Aware Interfaces: Spring Framework infrastructure provides several aware interfaces. These interfaces inculcate certain distinct behavior to a created bean. 

Some of the important Aware Interfaces include:

  • BeanFactoryAware: setBeanFactory() furnishes access to the bean factory that created the object.
  • BeanNameAware: setBeanName() method under BeanNameAware interface provides the name of the bean. 
  • ApplicationContextAware: setApplicationContext() method under this specific interface provides the ApplicationContext of the bean. 

PostConstruct and PreDestroy annotations: PostConstruct is an annotated method. It is called after bean construction and before requesting an object. PreDestroy is also an annotated method. It is called just before the destruction of a bean.

Following program depicts the usage of annotations

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy; 

public class BeanWork

{

   @PostConstruct

    public void createInit() 

    {

        //Initiation of bean(PostConstruct)

    }

   @PreDestroy

    public void createDestroy() 

    {

        //Destruction of bean(PreDestroy)

    }

}

The createInit() and createDestroy() are custom created initiation and destruction methods of a bean. This is done using the XML bean configuration file. 

Destruction of a bean

The last process in the spring bean life cycle is the destruction process. It is the process of removing a bean. The removal of a bean rests on JAVA or XML bean configuration file. 

This can be done in two ways

  • DisposableBean callback interface: Disposal is done in a method named destroy(). This method is present in org.springframework.beans.factory.DisposableBean interface. In the program below, a class is created which implements this interface. This enables using the destroy() method of that interface in the created class.

Following is the program depicting this instantiation process

import org.springframework.beans.factory.DisposableBean;

public class Destroyingbean implements DisposableBean

{

@Override

public void destroy() throws Exception

{

// Bean is destroyed

}

}

  • Custom created method of destruction in the bean configuration file: XML based configuration file is used here. The destroy-method of this file is used to specifically name the destruction method. This method is then used in the class for the destruction of the bean. The local definition of a single bean and steps to destroy it is shown below.

Beans.xml:

<beans>

<bean id=”destroyingbean” class=”com.workinginjava.work.Destroyingbean”

destroy-method=”createDestroy” ></bean>

</beans>

Following is the program depicting this destruction process by loading the beans.xml:

package com.workinginjava.work;

public class Destroyingbean 

{

public void createDestroy()

//Custom destruction method is used for the destruction of a bean

}

Spring beans are created for a specific purpose. So, every bean goes through a distinct spring life cycle. There are two ways of beginning and ending the spring bean lifecycle. If InitializingBean and DisposableBean interfaces are used, it binds the code to Spring. A better way is to identify the init-method and destroy-method in the bean configuration file.

Also Read: Spring Developer Salary in India: For Freshers & Experienced

Wrapping up

These points about the spring bean life cycle might answer some questions. Still, they raise new ones for you – What are the resources for an aspiring Full Stack developer and the use of spring framework? What is the scope in this field? And, most importantly, how to build a career in this domain?

If you’re interested to learn more about full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

Leave a comment

Your email address will not be published. Required fields are marked *

×