Spring Bean Life Cycle Part 2

Posted By :Keshav Agarawal |29th October 2019

Let see Bean lifecycle By implementing some spring interface

So far we have understood spring bean life cycle through spring configurations.
Now we have to do it using spring interfaces

Here we are going to use those 2 classes and config.xml (that we used in the previous blog, Click Here) i.e. Employee.java, TestApp.java, config.xml

We will not do any modification in TestApp.java. It will remain the same.
We will perform changes in our POJO class i.e. Employee.java

We are going to use these interface: (these interface will be implemented by POJO class)
1.) InitializingBean
2.) DisposableBean

Now POJO will override their method, as the rule is whenever a class implements an interface then it should override all its abstract methods.

Methods that need to be implemented or override:

1.) void afterPropertiesSet(): a method of InitializingBean
2.) void destroy(): a method of DisposableBean


public class Employee implements InitializingBean, DisposableBean {

    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;
    }

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

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

    public void afterPropertiesSet() throws Exception {
        System.out.println("Inside afterPropertiesSet method i.e. init");
    }

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

Now its time to perform changes in config.xml

As the previous config XML was this one:


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

Here we do not need to use init-method and destroy-method. (As we have already implemented those interface), so we can remove them. 
we will only configure the bean in config.xml


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

Spring will automatically identify the Bean is implemented these two interfaces i.e. InitializingBean and DisposableBean from spring and will invoke the afterPropertiesSet() and destroy() method.

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


    Inside constructor
    Inside setter method
    Inside afterPropertiesSet method i.e. init
    Employee id is: 123
    Inside destroy method

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


About Author

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

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us