PropertyPlaceHolderConfigurer usage in my app

Original usage

 

Originally, our application use the org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in this way.

 

In Spring config file:

 

  <bean id="initializationBean" class="com.citigroup.eqtg.exchangesim.InitializationBean">

    <property name="beanFactoryPostProcessor" ref="placeholderConfigurer"/>

  </bean>

  <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="searchSystemEnvironment" value="true"/>

    <property name="location" value="classpath:application.properties"/>

  </bean>

 

Something to explain:

 

1)    PlaceHolderConfigurer bean is used to separate env-dependant config/param from actual source code. Specific configuration is used in config file – application.properties.

And in source code, we just use ${database.jdbc.url}, ${database.jbdc.user}, etc.

 

2)    In application.properties

 

application.database.jdbc.url= ...

application.database.user= ...

 

3)    For Beanfactory, PlaceholderConfigurer won’t take effect automatically. You have to mantually coding in this way:

 

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("com/starxing/test/conf.xml"));

PropertyPlaceholderConfigurer cfg = …… // There are 2 ways to instantiate.

cfg.postProcessBeanFactory(factory); 

 

   After this, variables used in spring config has been replaced with real values.

 

4)    For ApplicationContext, there is no need to be this complex. As long as you’ve configged placeholderConfigurer in the spring config file, spring will replace the variables automatically. So, ApplicationContext is strongly preferred.

 

5)    In our application, we configged a initializationBean, the only purpose of this class is to call “cfg.postProcessBeanFactory(factory);”. To do this,

 

a.    It has to get a reference to the Spring BeanFactory, so, it has to implement the BeanFactoryAware interface.

b.    It has to get a reference to the PropertyPlaceholderConfigurer object, so it has to has be property --- beanFactoryPostProcessor.

c.    It has to have a time to call this, so it has to implement the InitializingBean interface.

 

public class InitializationBean implements BeanFactoryAware, InitializingBean {

    private BeanFactory beanFactory;

    private BeanFactoryPostProcessor beanFactoryPostProcessor;

 

    public InitializationBean() {

    }

 

    public void afterPropertiesSet() {

        beanFactoryPostProcessor.postProcessBeanFactory((ConfigurableListableBeanFactory)beanFactory);

    }

 

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

        this.beanFactory = beanFactory;

    }

 

    public void setBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor) {

        this.beanFactoryPostProcessor = beanFactoryPostProcessor;

    }

}

 

So, generally speaking, it’s really very complex the use Beanfactory + PropertyPlaceholderConfigurer.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值