<p>In this article, we will explore BeanFactoryPostProcessor in Spring. BeanFactoryPostProcessor works on the bean definitions or configuration meta data of the bean before beans are actually created. Spring provides multiple BeanFactoryPostProcessor beans, so it invoked to resolve run time dependencies such reading value from external property files. In Spring application, BeanFactoryPostProcessor can modify the definition of any bean.</p>
<h2>BeanFactoryPostProcessor in Spring</h2>
<p>If you define a BeanFactoryPostProcessor in one container, it will only be applied to the bean definitions in that container.</p>
<pre class="highlight">public interface BeanFactoryPostProcessor { 
 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory); 
} 
</pre>
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<h2 style="color: #274e13;"><b>Customizing beans with BeanPostProcessors</b></h2>
<p>In <a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/">previous chapter</a> we have been seen that how to <a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/"><b><i>Customizing beans with BeanPostProcessors:</i></b></a><br />
A bean post-processor is a java class which implements the <b>org.springframework.beans.factory.config.BeanPostProcessor</b> interface, which consists of two callback methods<br />
(<b><i>postProcessAfterInitialization(Object bean, String beanName)</i></b> &; <b><i>postProcessBeforeInitialization(Object bean, String beanName)</i></b>).</p>
<div id="ads-id" align="center"></div>
<p>When such a class is registered as a post-processor with the <b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory</a></b>, for each bean instance that is created by the <a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><b>BeanFactory</b></a>, the post-processor will get a callback from the <a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><b>BeanFactory </b></a>before any initialization methods (afterPropertiesSet and any declared init method) are called, and also afterwords.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p> ;</p>
<h2 style="color: #274e13;"><b>Customizing bean factories with BeanFactoryPostProcessors</b></h2>
<div style="color: #274e13;">A bean factory post-processor is a java class which implements the <b>org.springframework.beans.factory.config.BeanFactoryPostProcessor</b> interface. It is executed manually (in the case of the <b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory</a></b>) or automatically (in the case of the <b><a href="https://dineshonjava.com/application-context-in-spring/">ApplicationContext</a></b>) to apply changes of some sort to an entire <b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory</a></b>, after it has been constructed.</div>
<div style="color: #274e13;"> Spring includes a number of pre-existing bean factory post-processors, such as given below<br />
<b><br />
</b> <b>PropertyResourceConfigurer</b><b> </b>and <b>PropertyPlaceHolderConfigurer &#8211; </b>implemented as a bean factory post-processor, is used to externalize some property values from a <b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory </a></b>definition, into another separate file in Java Properties format. This is useful to allow to developer to declare some key property as properties file. As given below example show the database connection related information in the following property file.</div>
<div style="color: #274e13;"><b><br />
</b> <b>databaseConfig.properties</b></div>
</div>
<pre class="highlight">jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://davproductionDB:3306 
jdbc.username=root 
jdbc.password=root 
</pre>
</div>
<div style="color: #274e13;">
<p>Now in the bean configuration file has the <b><i>dataSource </i></b>bean as below.</p>
</div>
<pre class="highlight"><;bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">; 
 <;property name="driverClassName" value="${jdbc.driverClassName}">; 
 <;property name="url" value="${jdbc.url}">; 
 <;property name="username" value="${jdbc.username}">; 
 <;property name="password" value="${jdbc.password}">; 
<;/property>;<;/property>;<;/property>;<;/property>;<;/bean>; 
</pre>
<div style="color: #274e13;"><b><br />
</b> <b>To use this with a <a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory</a>, the bean factory post-processor is manually executed on it:</b><br />
<b><br />
</b></div>
<pre class="highlight">XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml")); 
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); 
cfg.setLocation(new FileSystemResource("databaseConfig.properties")); 
cfg.postProcessBeanFactory(factory); 
</pre>
<div style="color: #274e13;"><b><br />
</b> <b>To use this with a <a href="https://dineshonjava.com/application-context-in-spring/">ApplicationContext</a>, the bean factory post-processor is automatically executed on it:</b><br />
<b><br />
</b></div>
<pre class="highlight">ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
</pre>
<div style="color: #274e13;"></div>
<div style="color: #274e13;">Now see the full running application which using <b><i>BeanFactoryPostProcessor</i></b> interceptor to config the <b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/">BeanFactory</a></b> at time of initialization.</div>
<p><b>Triangle.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.beanFactoryPP.tutorial; 
 
public class Triangle 
{ 
 private Point pointA; 
 private Point pointB; 
 private Point pointC; 
 /** 
 * @param pointA the pointA to set 
 */ 
 public void setPointA(Point pointA) { 
 this.pointA = pointA; 
 } 
 
 /** 
 * @param pointB the pointB to set 
 */ 
 public void setPointB(Point pointB) { 
 this.pointB = pointB; 
 } 
 
 /** 
 * @param pointC the pointC to set 
 */ 
 public void setPointC(Point pointC) { 
 this.pointC = pointC; 
 } 
 
 public void draw() 
 { 
System.out.println("PointA is ("+pointA.getX()+", "+pointA.getY()+")"); 
System.out.println("PointB is ("+pointB.getX()+", "+pointB.getY()+")"); 
System.out.println("PointC is ("+pointC.getX()+", "+pointC.getY()+")"); 
 } 
} 
</pre>
<p><b><br />
</b> <b>Point.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.beanFactoryPP.tutorial; 
 
public class Point 
{ 
 private int x; 
 private int y; 
 /** 
 * @return the x 
 */ 
 public int getX() { 
 return x; 
 } 
 /** 
 * @param x the x to set 
 */ 
 public void setX(int x) { 
 this.x = x; 
 } 
 /** 
 * @return the y 
 */ 
 public int getY() { 
 return y; 
 } 
 /** 
 * @param y the y to set 
 */ 
 public void setY(int y) { 
 this.y = y; 
 } 
} 
</pre>
<div style="color: #274e13;">Now define the bean configuration file.</div>
<div style="color: #274e13;"></div>
<p><b>spring.xml</b></p>
<pre class="highlight"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:> 
<div style="color: #274e13;">In the above configuration file, bean id "<i><b>pointC</b></i>" have the two properties <i><b>x</b></i> and <i><b>y</b></i> the value of these two properties read from the property file (<i><b>shape.properties</b></i>)as given below</div> 
<div style="color: #274e13;"></div> 
<b>shape.properties</b> 
<pre class="highlight">pointC.x=20 
pointC.y=0 
</pre>
<div style="color: #274e13;">Now define a class which implement the <i><b>BeanFactoryPostProcessor </b></i>interface for the config the bean factory at the time of initialization with given property file value.</div>
<p><b><br />
</b> <b>BeanFactoryPPDemo.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.beanFactoryPP.tutorial; 
 
import org.springframework.beans.BeansException; 
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 
import org.springframework.core.io.FileSystemResource; 
 
public class BeanFactoryPPDemo implements BeanFactoryPostProcessor 
{ 
@Override 
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException 
 { 
 PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer(); 
 cfg.setLocation(new FileSystemResource("shape.properties")); 
 cfg.postProcessBeanFactory(beanFactory); 
 System.out.println("Bean factory post processor is initialized"); 
 } 
} 
</pre>
<div style="color: #274e13;">If every thing is Ok then run the following main application file and see the console.</div>
<div style="color: #274e13;"></div>
<p><b>DrawingApp.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.beanFactoryPP.tutorial; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class DrawingApp 
{ 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) 
 { 
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
Triangle triangle = (Triangle) context.getBean("triangle"); 
triangle.draw(); 
 } 
 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">Jul 5, 2012 12:43:27 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh</div>
<div style="color: red;">INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: startup date [Thu Jul 05 00:43:27 IST 2012]; root of context hierarchy</div>
<div style="color: red;">Jul 5, 2012 12:43:27 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions</div>
<div style="color: red;">INFO: Loading XML bean definitions from class path resource [spring.xml]</div>
<div style="color: red;">Jul 5, 2012 12:43:27 AM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties</div>
<div style="color: red;">INFO: Loading properties file from file [F:my workspacespring16BeanFactoryPPDemoshape.properties]</div>
<p><b>Bean factory post processor is initialized</b></p>
<div style="color: red;">Jul 5, 2012 12:43:27 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons</div>
<p><b>INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14c1103: defining beans [triangle,pointA,pointB,pointC,com.dineshonjava.sdnext.beanFactoryPP.tutorial.BeanFactoryPPDemo#0]; root of factory hierarchy</b><br />
<b>PointA is (0, 0)</b><br />
<b>PointB is (-20, 0)</b><br />
<b>PointC is (20, 0)</b></p>
</div>
</div>
<div style="color: #274e13;"></div>
<div style="color: #274e13;"><b>See the project structure for that application.</b></div>
<div></div>
<div class="separator" style="clear: both; text-align: center;"><img title="BeanFactoryPostProcessor in Spring" src="https://dineshonjava.com/wp-content/uploads/2012/07/beanfctorypp.jpg" border="0" /></div>
<p> ;</p>
<div style="color: #274e13;"></div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Related Topics you may like</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/interview-questions-on-spring/"><span style="color: red;">Spring Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-rest-web-services-interview-questions-and-answers/"><span style="color: red;">Spring REST Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-interview-questions-and-answers/"><span style="color: red;">Spring Boot Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-boot-microservices-interview-questions-answers/"><span style="color: red;">Spring Boot Microservices Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/dependency-injection-in-spring/"><span style="color: red;">Dependency Injection (DI) in Spring </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-ioc-container/"><span style="color: red;">Spring IoC Container</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-bean-factory-in-spring/"><span style="color: red;">What is Bean Factory in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/application-context-in-spring/"><span style="color: red;">ApplicationContext in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-autowiring-in-spring/"><span style="color: red;">Bean Autowiring in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-bean-scopes/"><span style="color: red;">Spring Bean Scopes</span></a></b></li>
<li><b><a href="https://dineshonjava.com/create-custom-bean-scope-in-spring-example/"><span style="color: red;">Create Custom Bean Scope in Spring Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/using-applicationcontextaware-in-spring/"><span style="color: red;">Using ApplicationContextAware in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/bean-lifecycle-and-callbacks/"><span style="color: red;">Spring Bean Life Cycle and Callbacks</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/"><span style="color: red;">BeanPostProcessor in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/annotations-in-spring-and-based/"><span style="color: red;"> Annotations in Spring and Based Configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-jsr-250-annotations/"><span style="color: red;">Spring JSR-250 Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/jsr-330-annotations-in-spring/"><span style="color: red;">JSR 330 Annotations in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/method-injection-with-spring-using-lookup-method-property/"><span style="color: red;">Method injection with Spring using Lookup method property</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/writing-beanpostprocessor-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/coding-to-interfaces-in-spring/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '661'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…