Real world advantage of using Spring BeanFactoryPostProcessor

Viewed 152

I understand the usage of BeanFactoryPostProcessor where we can modify metadata of bean definitions by writing our custom class which implements BeanFactoryPostProcessor. But I fail to understand Why or What is the advantage of changing metadata value via BeanFactoryPostProcessor when I can simple change it in the spring xml itself.

2 Answers
  • Lets take PropertyPlaceholderConfigurer BeanFactoryPostProcessor as an example which spring itself uses. It allows the following bean definition modified before it becomes the concrete bean definition (.ie ${jdbc.url} replaced with the correct value )
   <bean id="dataSource">
     <property name="url" value="${jdbc.url}" />
   </bean>
  • Without it, you have to have many different bean definitions for each environment like test, stage, production and different profiles to activate them. And then you have security issue because you have to store the production database url in the definition.

  • This is just one simple example, there are so many BeanFactoryPostProcessor implementations spring itself uses. Check them through

You may want to modify spring config in libraries you pull in and have no control over :)

Related