Monday 1 May 2017

Spring: BeanPostProcessor example

BeanPostProcessor interface provide call back methods, by implementing BeanPostProcessor interface you can provide your own instantiation logic, dependency-resolution logic, and so forth.

Following table summarizes the methods provided by BeanPostProcessor interface.
Method
Description
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
              
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks like InitializingBean's afterPropertiesSet or a custom init-method.
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method.

Find the following complete working application.

Author.java
package com.sample.pojo;

import org.springframework.context.Lifecycle;

public class Author implements Lifecycle {
 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();
 }

 @Override
 public void start() {
  System.out.println("in start");
 }

 @Override
 public void stop() {
  System.out.println("in stop");
 }

 @Override
 public boolean isRunning() {
  System.out.println("in running");
  return true;
 }

}

CustomBeanPostProcessor.java
package com.sample.pojo;

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

public class CustomBeanPostProcessor implements BeanPostProcessor{

 @Override
 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("Before Initializing Bean " + beanName + ", " + bean);
  return bean;
 }

 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  System.out.println("After Initializing Bean " + beanName + ", " + bean);
  return bean;
 }

}

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.CustomBeanPostProcessor" />
 
</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
Before Initializing Bean osho, Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
After Initializing Bean osho, Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
in running
in stop


Previous                                                 Next                                                 Home

No comments:

Post a Comment