Home  >  Spring Core

Example of BeanFactoryPostProcessor in Spring

By Arvind Rai, April 22, 2013
BeanFactoryPostProcessor in spring belongs to the package org.springframework.beans.factory.config. BeanFactoryPostProcessor is used to work on bean configuration metadata. BeanFactoryPostProcessor can change bean configuration metadata potentially. It happens before other bean is initialized by spring container. In spring, more than one BeanFactoryPostProcessor can be used and their order of execution can be set by order attribute.
BeanFactoryPostProcessorTest.java
package com.concretepage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
			throws BeansException {
		System.out.println("Inside postProcessBeanFactory method.");
	}
} 

app-conf.xml
<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 class="com.concretepage.BeanFactoryPostProcessorTest"  />
    <bean id= "testBean" class="com.concretepage.TestBean" />
</beans> 

TestBean.java
package com.concretepage;
public class TestBean {
	public TestBean(){
		System.out.println("Object of TestBean is created.");
	}
} 

SpringTest.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");
		context.registerShutdownHook();
	}
}  

Output
Inside postProcessBeanFactory method.
Object of TestBean is created. 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us