BeanPostProcessor in Spring
As the name indicate, BeanPostProcessor are classes that tells spring to do some action while initializing the bean. The BeanPostProcessor interface defines callback methods that you can implement to process fields that were set, perform validation on a bean etc. You can also implement some custom logic after the Spring container finishes instantiating.
BeanPostProcessor interface has two methods.
- public Object postProcessAfterInitialization(Object arg0, String arg1)
postProcessAfterInitialization method accept two parameters , First is the bean object and second is the Bean Name .This method returns the Bean object
- public Object postProcessBeforeInitialization(Object arg0, String arg1)
postProcessBeforeInitialization method accept two parameters , First is the bean object and second is the Bean Name .This method returns the Bean object
Here in this article lets see how to configure the BeanPostProcessor with an example . Lets create a person class which has two member variables name ,age and a method display() to print the name and age values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.j2eereference.spring; public class Person { 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 display(){ System.out.println("Name is "+name); System.out.println("Age is "+age); } } |
Spring.xml :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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"> <bean id="personBean1" class="com.j2eereference.spring.Person"> <property name="name" value="George"/> <property name="age" value="30"/> </bean> <bean id="personBean2" class="com.j2eereference.spring.Person"> <property name="name" value="Leo"/> <property name="age" value="25"/> </bean> <bean class="com.j2eereference.spring.DisplayNameBeanPostProcessor"> </bean> </beans> |
Here you can notice that we have added a bean ,com.j2eereference.spring.DisplayNameBeanPostProcessor .
Now lets create the class DisplayNameBeanPostProcessor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.j2eereference.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class DisplayNameBeanPostProcessor 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; } } |
In this class we have to impement BeanPostProcessor . I have implemented postProcessAfterInitialization and postProcessBeforeInitialization methods to print the respective messages with bean name .
Now lets test this configuration with the below code
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.j2eereference.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String[] args) { ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml"); Person person= (Person)context.getBean("personBean1"); person.display(); } } |
Output
May 19, 2015 4:33:43 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@71c2812: startup date [Tue May 19 16:33:43 IST 2015]; root of context hierarchy
May 19, 2015 4:33:43 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
This is before Initializing bean personBean1
This is After Initilizing bean personBean1
This is before Initializing bean personBean2
This is After Initilizing bean personBean2
Name is Leo
Age is 25
- postProcessAfterInitialization and postProcessBeforeInitialization methods are getting called while spring initialize the beans.
- postProcessAfterInitialization and postProcessBeforeInitialization methods are getting called for each and every bean configured in the spring.xml
- You can have any number of BeanPostProcessor.
Leave a Reply