Spring Bean Life Cycle Part 1

Posted By : Keshav Agarawal | 26-Sep-2019

Spring provides 2 lifecycle method to every bean creates. They are:

1. public void init() method
2. public void destroy() method

The names need not be the same, but the signature must be same. i.e. public void and no parameters.

You can define loading configuration from the file system or connecting to the database or connecting to web services or to the server can be written inside this init() method.

All the cleanup code or closing the connection should be written inside the destroy() method.
 

The lifecycle looks as follows:

  • Give a spring bean and XML configuration to the Spring Container.
  • The spring container will first instantiate the object of that bean then it will set all the values on that bean object then it will invoke the init method().
  • So whatever code we put in the init() method will be executed after the set into the object.
  • Then it will read that bean or make use of that bean into the application.
  • Once it is done and before the container is destroyed or the object is destroyed, the destroy() method will be invoked or before the destruction of the object, the destroy()  method will be invoked.
  • So put all the cleanup code in destroy() method.

(Note: Image is taken from this link. You can consider this Business object as Bean object)

We can configure the lifecycle method in 3 ways:
1. Using XML configuration
2. By implementing some spring interface
3. Annotations.

 

Let see Bean lifecycle using XML configuration:
These are the few steps that we are going to follow:
1. Create the bean as Employee
2. Create the configuration
3. Test application.

 

1. Create the bean as Employee :

Create a public class Employee and declare some variables and setters and getters method for it.

Example:

public class Employee {

    private int id;

    public Employee() {
        System.out.println("Inside constructor");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        System.out.println("Inside setter method");
        this.id = id;
    }
}

Define the init() method and destroy() method as well inside this bean class.

    public void init(){
        System.out.println("Inside init method");
    }

    public void destroy(){
        System.out.println("Inside destroy method");
    }

2. Create the configuration:

Create a configuration file as config.xml. And provide the configuration for bean class as:

    <bean class="fully qualified name of class" name="name of bean object" init-method="name of init method" destroy-method="name of destroy method">
        <property name="varible name" value="value for the variable"></property>
    </bean>
 

So let's provide the bean configuration for Employee as mentioned below.

    <bean class="com.bean.lifecycle.Employee" name="employee" init-method="init" destroy-method="destroy">
        <property name="id" value="123"></property>
    </bean>
 

3. Test application.

Now we will create a Test application where we will initialize Spring container and then will get the Bean object. 

public class TestApp {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("Config.xml");
        Employee employee = (Employee)context.getBean("employee");
        System.out.println("Employee id is: "+employee.getId());

    }
}

When this application will run, it will produce the following result.

Inside constructor
Inside setter method
Inside init method
Employee id is: 123

Here we can observe that destroy() method is not invoked.

The reason is, destroy() method will invoke when the container is destroyed. 

To destroy the container or shut down the container, we need to call: registerShutdownHook() method.

It is a method of AbstractApplicationContext class which is an abstract class.

So we need to modify our Test application code as:

        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Config.xml");
        Employee employee = (Employee)context.getBean("employee");
        System.out.println("Employee id is: "+employee.getId());
        context.registerShutdownHook();

Now again when this application will run, it will produce the following result:

Inside constructor
Inside setter method
Inside init method
Employee id is: 123
Inside destroy method

Thanks for reading this amazing concept. Stay tuned for the remaining 2 more ways to understand the life cycle of a bean.

About Author

Author Image
Keshav Agarawal

Keshav Agarawal is Java Developer with skilled in Java, Servlet, JSP, JDBC, Spring Boot, Spring MVC, JPA, Hibernate. His hobbies are playing chess and reading books in free time. By nature he is friendly and honest person.

Request for Proposal

Name is required

Comment is required

Sending message..