Tuesday 2 May 2017

Spring: configure multiple BeanPostProcessors

In my previous post, I explained how to configure BeanPostProcessor. Spring allows you to configure multiple bean post processors.

Find the following complete working application.

CustomBeanPostProcessor1.java
package com.sample.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class CustomBeanPostProcessor1 implements BeanPostProcessor{

 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor1");
  return bean;
 }

 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor1");
  return bean;
 }

}

CustomBeanPostProcessor2.java
package com.sample.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class CustomBeanPostProcessor2 implements BeanPostProcessor {

 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor2");
  return bean;
 }

 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor2");
  return bean;
 }

}

Author.java
package com.sample.pojo;

public class Author{
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public Author() {
  System.out.println("Author Constructor called");
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}


myConfiguration.xml
<?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.xsd"
 default-init-method="init" default-destroy-method="cleanup">

 <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>
 
 <bean class="com.sample.pojo.CustomBeanPostProcessor1" />
 <bean class="com.sample.pojo.CustomBeanPostProcessor2" />
 
</beans>

HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Author;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Author author = context.getBean("osho", Author.class);

  System.out.println(author);

  ((ClassPathXmlApplicationContext) context).close();
 }
}


Output
Author Constructor called
Inside CustomBeanPostProcessor1
Inside CustomBeanPostProcessor2
Inside CustomBeanPostProcessor1
Inside CustomBeanPostProcessor2
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]

By implementing the Ordered interface, you can specify calling order.

CustomBeanPostProcessor1.java
package com.sample.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;

public class CustomBeanPostProcessor1 implements BeanPostProcessor, Ordered{

 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor1");
  return bean;
 }

 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor1");
  return bean;
 }

 @Override
 public int getOrder() {
  return 2;
 }

}

CustomBeanPostProcessor2.java
package com.sample.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;

public class CustomBeanPostProcessor2 implements BeanPostProcessor, Ordered  {

 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor2");
  return bean;
 }

 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Inside CustomBeanPostProcessor2");
  return bean;
 }

 @Override
 public int getOrder() {
  return 1;
 }

}

Re run HelloWorld application, you can able to see following output.
Author Constructor called
Inside CustomBeanPostProcessor2
Inside CustomBeanPostProcessor1
Inside CustomBeanPostProcessor2
Inside CustomBeanPostProcessor1
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]



Previous                                                 Next                                                 Home

No comments:

Post a Comment