How to Use Init and Destroy Method in Spring Java Application

Posted on

Introduction

This article is focusing on how to use a certain method to demonstrate the life cycle of a Java Spring Bean. The demonstration of the Java Spring Bean life cycle is possible by using two methods available. Those methods are the init method which is defining the start of the life cycle of a Java Spring Bean. The next one is the destroy method which is defining the end of the life cycle of a Java Spring Bean. There are several requirements for using those methods. First of all, those methods must be exist in the Java Spring configuration file. Additionally, the Java Spring Bean need to define it too. This article is using a base article exist in article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link.

How to Use Init and Destroy Method in Spring Java Application

This part will be split into two more parts. The first one is the part for preparing the source code for demonstrating it. The last one is the part for explaining the source code after the execution.

Preparing the Init and Destroy Method in Spring Java Application

So, what are the steps for defining all the methods which is necessary to describe or demonstrate the life cycle of a Java Spring Bean. Just perform the following steps :

  1. First of all, access the Java Spring configuration file. As an example, the file exist in ‘src/main/resources’ with the name of ‘spring-config.xml’. The following is the content in order to use init and destroy method in a Java Spring Bean :

    <?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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <bean id="employeeBean" class="com.test.spring.Employee" init-method="init" destroy-method="destroy">
        </bean>
    </beans>
  2. Next, add the init and destroy method to the Java Spring Bean file with the name of ‘Employee’ as in the above definition. The content will be exist as follows :

    package com.test.spring;
    
    import java.util.List;
    
    public class Employee {
         private String id;
    
         public void setId(String id) {
              this.id = id;
         }
    
         public void init(){
              System.out.println("Employee is going through the init method ... ");
         }
    
         public void destroy(){
              System.out.println("Employee is going through the destroy method ... ");
         }
    
         public void identifyID(){
              System.out.println("I am an "+this.id+" !");
         }
    }
  3. Soon after, edit the main file for running the program so that it will have the definition of the init and destroy method as follows :

    package com.test.spring;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class SpringMain {
           public static void main(String[] args){
                  ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
                  ((AbstractApplicationContext) context).registerShutdownHook();
    
                  Employee employee = (Employee) context.getBean("employeeBean");
                  employee.setId("Employee of the Company");
                  employee.identifyID();
           }
    }

Running the Spring Java Application

Following after the above part, just execute the command for running the main program. Below is the output of the execution :

How to Use Init and Destroy Method in Spring Java Application
How to Use Init and Destroy Method in Spring Java Application

So, right after the instantiation or the creation process of the object Employee, it will right away execute the init method. The main indication of the execution is the appearance of the printed string as exist in the init method as follows :

Employee is going through the init method ...

Soon after, there is a line of source code which is closing the context variable with the type of ApplicationContext. It is the following line :

((AbstractApplicationContext) context).registerShutdownHook();

The above line will instruct to close the context variable with the type of ApplicationContext. As the closing process of the variable, it will directly terminate all of the defined Java Spring Bean in the Java Spring Configuration file associated with the it which is in this context the variable with the type of ApplicationContext.

It will directly call the destroy method which in turn print the string exist as in thee destroy method as follows :

Employee is going through the destroy method ...

But, before executing the destroy method by printing the above string, it will execute all the available method related with the object or the Java Spring Bean ’employee’ first. It will print the following string as exist in the method ” as follows :

I am an Employee of the company !

So, the following is the output of the printable string of the method which is also representing the sequence of the method executed. First of all the init method, following are all the methods which is being called from the Java Spring Bean before in the end executing the destroy method. The output exist as follows :

Employee is going through the init method ...
I am an Employee of the company !
Employee is going through the destroy method ...

Changing the sequence of line containing the registerShutdownHook() with all the other callable method of the Java Spring Bean does not change the output. But the most important thing is the first line must be the line instantiating or creating the Java Spring Bean.

One thought on “How to Use Init and Destroy Method in Spring Java Application

Leave a Reply