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.

 

 

Now usage

 

There is no InitializationBean. And we just config the placeholderConfigurer in the spring config file, and it will work.

你可能感兴趣的:(spring,bean,xml,jdbc)