Bean Life Cycle in Spring

In the previous article, we have started to learn about Spring Bean Definition, bean Scopes. Now, In this article, we will understand the life cycle of a Spring Bean.

The Lifecycle of any bean means how the bean is created, how it goes from one stage to another, what are the stages involved from creation to destruction of any object or bean. In the same way, we have a bean life cycle in the spring framework. There are some stages involved in the Spring bean life cycle that we will see in this article.

What is Spring Bean LifeCycle?

As we know, Beans are nothing but objects in Spring. IOC container in Spring is responsible for creating objects from the configuration metadata that we supply in a config file. The container is responsible to manage the overall life cycle of the Spring beans i.e from the creation of a bean to its destruction. Following is the diagrammatical representation of Spring Bean Life cycle process flow.

  • Firstly, the Spring Container creates the object of the bean. Here, Bean gets instantiated.
  • Then, the Dependencies are Injected using XML or annotation-based configuration.
  • Now, the init() method is called where the initialization is done. for eg., if you have any connection to initialize you can initialize them inside this method.
  • Now, we read the bean and use them and call the respective methods.
  • At last, the destroy() method is called for some clean-up process and then the beans are destroyed.

Thus this is the process flow of Bean Life Cycle in Spring Bean. The important thing here is that the init() method is called after the properties are set i.e after injecting the dependencies.

Let us take an example to demonstrate the Bean Life cycle in Spring.

Example to understand flow of  Bean Life Cycle

There are 3 ways in which we can implement Bean Life Cycle:

  • By using XML Based Configuration
  • By using Annotation Based Configuration
  • By using Java Based Configuration

In this example, we will implement using XML Based Configuration.

Create a Spring Project Go to File> New > Other > Search maven > Select Maven Project > Next > Search Filter org.apche.maven.archetypes/webapp > Next > Enter Group Id & Archetype id > Finish.

  • Create a class Animal having a name attribute and a setter & getter method.
  • Add two methods of the life cycle: init() method and destroy() methods.

Animal.java

package com.codedec.di;

public class Animal {

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void show() {
    System.out.println("Name of the Animal is : " + name);
  }

  public void init() {
    System.out.println("<----------Init method is called---------->");
  }

  public void destroy() {
    System.out.println("<----------Destroy method is called------------>");
  }

}
  • create a beans.xml file with <bean> element having init-method and destroy-method.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="animal" class="com.codedec.di.Animal" init-method="init" destroy-method="destroy">
    <property name="name" value="Lion"></property>
  </bean>
</beans>
  • Create an App.java file. Here, Now we are not using Application Context Interface because it does not have an implementation of destroy() method.
  • In this, we will use¬†AbstractApplicationContext Interface and will call the¬†registerShutdownHook() method to call destroy() method.

App.java

package com.codedec.di;


import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
     Animal a1= context.getBean("animal",Animal.class);
     a1.show();
     //When I want to call destroy()
     context.registerShutdownHook();
  }

}

Output

<----------Dependencies Injected------------>
<----------Init method is called---------->
Name of the Animal is : Lion
<----------Destroy method is called------------>

Now, here we can see First the bean is created > then Dependencies is Injected > init is called > show() method is called> at last destroy is called.

Note: You can also change the name of init() and destroy() method. Whatever name you give just add the same name in the beans.xml file.

Thus, this was all about the Bean Life cycle in Spring Framework.