Spring BeanPostProcessors – Example Using  Eclipse IDE

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

1. Objective

In the previous articles, we have learned about Spring Bean life cycle. Now in this Spring Framework Tutorial, we will see what is Spring Bean Post Processors. The BeanPostProcessors helps you to do some operations before and after the creation of Spring Bean. In addition, we will see a Spring BeanPostProcessors example using Eclipse IDE so as to get a better idea about the post processors.

So, let’s see Spring Bean Post Processors.

Spring BeanPostProcessors - Latest Tutorial 2018

Spring BeanPostProcessors – Latest Tutorial 2018

2. Spring Bean Post Processors

The interface called BeanPostProcessors in Spring Framework is used for defining the callback methods which you can implement. So as to provide your own instantiation logic, dependencies resolution etc. Also, you are can implement some other custom logic after the initialization, configuration etc. of the Spring Beans by plugging more than Bean Post Processor implementations.

Do you know Why Spring Framework is Popular

You are allowed to configure more than one BeanPostProcessors interface and can control the order of execution of the BeanPostProcessors interface. This can be done by setting the order property given with the implementation of an Ordered interface by BeanPostProcessors.

The interface BeanPostProcessors operates on the instances of Bean. That is the Spring IoC container instantiates bean instance after which the BeanPostProcessors interfaces do their work.
The methods you studied in Bean lifecycle init() and destroy() are different than the Spring BeanPostProcessors. The BeanPostProcessor in Spring Framework is common to all of the beans.

3. Spring BeanPostProcessors Example Using  Eclipse IDE

The following is the example application with the steps using Eclipse IDE in which a class is created which implements BeanPostProcessors interface along with the two methods of it:

  1. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
  2. Add the Spring Libraries that are required using the Add External JARs options.
  3. Create HelloWorld.java, InitHelloWorld.java and MainApp.java under the above-made package.
  4. Write the Beans.xml configuration file under the src folder.
  5. Finally, write code for all Java files and Bean config file and run the application as described.

Read about Spring Batch Tutorial in detail

The code for HelloWorld.java is as follow:

package com.example; 
public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
   public void init(){
      System.out.println("Bean is going through init.");
   }
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

The example is a basic implementation of BeanPostProcessors which will print a Bean name before and after the initialization of the Bean. You have the free hand of writing any code before and after the initialization of the bean as you have the access on the object inside both the post processor methods.

a. Following is the InitHelloWorld.java

package com.example; 
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
   public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {    
      System.out.println("BeforeInitialization : " + beanName);
      return bean;  // you can return any other object as well
   }
   public Object postProcessAfterInitialization(Object bean, String beanName)
      throws BeansException {    
      System.out.println("AfterInitialization : " + beanName);
      return bean;  // you can return any other object as well
   }
}

The following is the MainApp.java file which has registerShutHook() declared on class AbstractApplicationContext:

package com.example; 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
      context.registerShutdownHook();
   }
}

Let’s discuss the Spring Bean Autowiring with Modes

b. The configuration file for Beans.xml is as defined

<?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 = "helloWorld" class = "com.example.HelloWorld"
      init-method = "init" destroy-method = "destroy">
      <property name = "message" value = "Hello World!"/>
   </bean> 
   <bean class = "com.example.InitHelloWorld" /> 
</beans>

If you are done with the creation of the code and configuration files. After everything is working fine run the application you will see the following message as output:

BeforeInitialization: HelloWorld

Bean is going through init.

AfterInitialization: HelloWorld

Your Message: Hello World!

Bean will destroy now.

So, this was all about Spring BeanPostProcessors. Hope you like our explanation.

4. Conclusion

Hence, in this session, we have learned Bean Post Processor in Spring Framework. This article had the implementation of the interface BeanPostProcessor and its two methods. Along with that, you had the working example using the Eclipse IDE. In our next tutorial, we will discuss Spring-Bean Definition Inheritance in detail. Furthermore, if you have any query, feel free to ask in the comment section.

Related Topic- Spring Framework Career Opportunity

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *