How to Use InitializingBean and DisposableBean Interface to define init and destroy method of Java Spring Bean

Posted on

Introduction

Another article for defining the init and destroy method of a Java Spring Bean. This article relates with the other previous articles. Those articles are also focusing on how to define the init and destroy method of a Java Spring Bean. The first one is using Java Spring Configuration file. It is defining an attribute of ‘init-method’ and ‘destroy-method’ in each of the Java Spring Bean definition. Just read the article with the title of ‘How to Use Init and Destroy Method in Spring Java Application’ in this link.

The second one is an article with the title of ‘How to Define Init and Destroy as Default Method in Spring Java Application’ in this link. In that article, the attribute of ‘init-method’ and ‘destroy-method’ is not in each of the Java Spring Bean definition. It will take a lot of effort if there are too many of them. Instead, the attribute of ‘default-init-method’ and ‘default-destroy-method’ is defined in the root Java Spring Bean definition. So, all the inner bean definition inside of it will have the same behaviour or rule.

Continue on to the third one is also an article with the similar content. It is an article with the title of ‘How to Use PostConstruct and PreDestroy Annotation for defining init and destroy method of Java Spring Bean’ and it is exist in this link.This article also has a similar purpose. Rather than using the Java Spring Configuration file, it directly edit the Java Spring Bean. In this context, it is directly add annotations to the init method and destroy method exist in the Java source code. The annotation ‘PostConstruct’ for the init method and the annotation ‘PreDestroy’ for the destroy method.

Use InitializingBean and DisposableBean Interface to define init and destroy method

Thus, with the similar purpose, this article will show how to define init and destroy method using another way. It is by using an interface class. The Java Spring Bean will implement the interfaces. The result of implementing the interfaces are the obligation to implement all abstract methods. Implementing one abstract method from the InitializingBean for defining the init method.

On the other hand, implementing another abstract method from the DisposableBean for defining the destroy method. For the purpose of simplicity, this article will use a base application exist in the article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link. The following are the steps to be able to do it :

  1. First of all, implement the InitializingBean for defining the init method. Just edit the Java Spring Bean directly. The following is the original Java Spring Bean with the name of ‘Employee.java’ as an example :

    package com.test.spring;
    
    public class Employee {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
        public void identifyID(){
            System.out.println("I am an "+this.id+" !");
        }
    }
  2. Following after, the first one is implementing the InitializingBean. Just edit the above Java Spring Bean and add the ‘implements InitiliazingBean’. It will force to implement all the available abstract methods. In this case, it is the method with the name of ‘afterPropertiesSet()’ as follows :

    package com.test.spring;
    
    import org.springframework.beans.factory.InitializingBean;
    
    public class Employee implements InitializingBean {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
        public void identifyID(){
            System.out.println("I am an "+this.id+" !");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

    Just modify the afterPropertiesSet method. Fill it with just a System.out.println statement in order to prove the execution of the command. The modification exist as follows :

    @Override
    public void afterPropertiesSet() throws Exception {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Employee is going through the afterPropertiesSet method ... ");
    }
    
  3. Next, implement the second one where it is an interface with the name of ‘DisposableBean’.

    package com.test.spring;
    
    import java.util.List;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    public class Employee implements InitializingBean, DisposableBean {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
        public void identifyID(){
            System.out.println("I am an "+this.id+" !");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            System.out.println("Employee is going through the afterPropertiesSet method ... ");
        }
    
        @Override
        public void destroy() throws Exception {
            // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }

     

    Just modify the above destroy method by filling it with the print statement. It is very useful for checking whether or not there is an execution of that destroy method. Modify it just like this as an example :

    @Override
    public void destroy() throws Exception {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Employee is going through the destroy method ... ");
    }
    
  4. Overall, the Java Spring Bean will have the following kind of snippet code :

    package com.test.spring;
    
    import org.springframework.beans.factory.InitializingBean;
    
    public class Employee implements InitializingBean {
        private String id;
    
        public void setId(String id) {
            this.id = id;
        }
        public void identifyID(){
            System.out.println("I am an "+this.id+" !");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            System.out.println("Employee is going through the afterPropertiesSet method ... ");
        }
        @Override
        public void destroy() throws Exception {
            // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            System.out.println("Employee is going through the destroy method ... ");
        }
    }
  5. While the main Java class file for running the Java Spring Bean will have the following content :

    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();
        }
    }
  6. Last but not least, execute the main program. It will actually generate the same output with the other way for defining init and destroy method for further usage throughout the Java Spring Bean life cycle. The following image is the output of the execution :

    How to Use InitializingBean and DisposableBean Interface to define init and destroy method of Java Spring Bean
    How to Use InitializingBean and DisposableBean Interface to define init and destroy method of Java Spring Bean

Leave a Reply