Spring Bean PostProcessors

  • Facebook
  • Google
  • LinkedIn
  • Twitter

BeanPostProcessor is the interface which defines callback methods that you can implement to provide your own instantiation logic, dependency logic etc, Conditional logic can be checked before bean registration to container. This allows you to add some custom logic before and after spring bean creation. Now you can implement these two methods in BeanPostProcessor class.

BeanPostProcessor class has two methods.

1: postProcessBeforeInitialization: It is used to make sure required actions are taken before initialization. e.g. you want to load certain property file, read data from the remote source or service.

2: postProcessAfterInitialization:It is used if you want to do after initialization before bean reference is given to application.

BeanPostProcessor Example

package com.javatutsworld;

public class Student {

	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}	
	public void Info(){
		System.out.println("Name is  "+name);
		System.out.println("Age is  "+age);
	}
}

applicationContext.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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
 <bean id="studentBean1" class="com.javatutsworld.Student">
	<property name="name" value="Ankur"/>
	<property name="age" value="40"/>
 </bean>
<bean id="studentBean2" class="com.javatutsworld.Student">
	<property name="name" value="Shariq"/>
	<property name="age" value="30"/>
 </bean>
 	
</beans>

InfoBeanPostProcessor.java

package com.javatutsworld;

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

public class InfoBeanPostProcessor implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {
		System.out.println("This is after Initilizing bean "+beanName);
		return bean;
	}

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

}

MainTest.java

package com.javatutsworld;

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

public class MainTest {
	public static void main(String[] args) {
		ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");	
		Student student= (Student)context.getBean("studentBean1");
		student.Info();
		Student student2= (Student)context.getBean("studentBean2");
		student2.Info();
		
	}
}

Output

Nov 6, 2015 6:11:56 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@535b58: startup date [Fri Nov 06 06:11:56 EST 2015]; root of context hierarchy
Nov 6, 2015 6:11:56 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Nov 6, 2015 6:11:57 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@56f631: defining beans [studentBean1,studentBean2,com.javatutsworld.InfoBeanPostProcessor#0]; root of factory hierarchy
This is before Initializing bean studentBean1
This is after Initilizing bean studentBean1
This is before Initializing bean studentBean2
This is after Initilizing bean studentBean2
Name is  Ankur
Age is  40
Name is  Shariq
Age is  30
      Advertisements