How to Use BeanPostProcessor in Java Spring Bean Application

Posted on

Introduction

This is an article where the main content is to show how to use BeanPostProcessor in a Java Spring Application. In summary, BeanPostProcessor is an interface exist in the Java Spring Framework. It is very useful for defining several methods for further execution in the Java Spring Bean life cycle. Whenever a class implementing the BeanPostProcessor, it will require further override process for several methods. Those methods are the postProcessBeforeInitialization and the postProcessAfterInitialization.

The postProcessBeforeInitialization method has a certain period of time of execution in the life cycle of the Java Spring Bean. It is exactly after the instantiation process of an object, before executing or invoking the init method. On the other hand, the postProcessAfterInitializaiton method also has a certain period of time of execution in the life cycle of the Java Spring Bean. It is exactly right after executing or invoking the init method.

This article will show it in the form of a snippet code to demonstrate the sequence of the execution for each of the method. The base article exist from the previous article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link.

How to Use BeanPostProcessor in Java Spring Bean Application

The following are the steps for implementing or using BeanPostProcessor in a Java Spring Bean Application :

  1. Basically, just add a new class for implementing the BeanPostProcessor interface. The following is the content of the Java class file with the name of EmployeeBeanPostProcessor as an example :

    package com.test.spring;
    
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class EmployeeBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object args, String status){
             System.out.println("Before executing the init method of the "+args+" object");
             return args;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object args, String status){
             System.out.println("After executing the init method of the "+args+" object");
             return args;
        }
    }
  2. Do not forget to define the init method. Just do it as in the article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link. So, in the Java Spring Bean with the name of ‘Employee’ as an example, the following is the content with the definition of the init method :

    package com.test.spring;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    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. Moreover, there must be a necessary step for defining the init method in the Java Spring Configuration file and also the Java file class which is implementing the BeanPostProcessor as follows :

    <?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>
         <bean class="com.test.spring.EmployeeBeanPostProcessor" />
    </beans>
  4. Before executing the main Java program file, the following is the actual content of the Java program file itself :

    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();
          }
    }
  5. Finally, execute the above main Java program file. The following output will appear as in this image below :

    How to Use BeanPostProcessor in Java Spring Bean Application
    How to Use BeanPostProcessor in Java Spring Bean Application

    Actually, the above output are representing the sequence of the method execution through the life cycle of a Java Spring Bean. Soon after an object is created or instantiated, it will starts with the execution of the method of postProcessBeforeInitialization. It will print the string exist in that method. That is the ‘Before executing the init method of the ….. object’.

    Following after, the init method of the Java Spring Bean will get the turn for the execution. In this example, the string is ‘Employee is going through the init method …’. Soon after, the turn for further execution will be for the postProcessAfterInitialization. The execution in that method is printing the string of ‘After executing the init method of the … object’.

    Last but not least, all the available methods which is called or invoked in the program. In this example, the representation is the method with the name of ‘identifyID’. It will print the string ‘I am an Employee of the Company !’. Finally, the last one is executing the destroy method which in this example is printing ‘Employee is going through the destroy method ….’. That is all the method available for the execution throughout the life cycle of the Java Spring Bean with the name of ‘Employee’.

Leave a Reply