spring中使用外部属性文件(关于PropertyPlaceholderConfigurer)

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean id="propertyConfigurer"         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">         <property name="location">             <value>classpath:com/starxing/test/jdbc.properties</value>         </property> <!--  使用locations属性定义多个配置文件        <property name="locations">             <list>                 <value>classpath:config/maxid.properties</value>                 <value>classpath:config/jdoserver.properties</value>             </list> </property>   -->     </bean>     <bean id="dataSource"         class="org.springframework.jdbc.datasource.DriverManagerDataSource">         <property name="url">             <value>${database.url}</value>         </property>         <property name="driverClassName">             <value>${database.driver}</value>         </property>         <property name="username">             <value>${database.user}</value>         </property>         <property name="password">             <value>${database.password}</value>         </property>     </bean> </beans> 3.Config.java package com.starxing.test; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.FileSystemResource; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class Config {     public static void main(String[] args) {         XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(                 "com/starxing/test/conf.xml"));         // 如果要在BeanFactory中使用,bean factory post-processor必须手动运行:         PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();         cfg.setLocation(new FileSystemResource(                 "com/starxing/test/jdbc.properties"));         cfg.postProcessBeanFactory(factory);         DriverManagerDataSource dataSource = (DriverManagerDataSource) factory                 .getBean("dataSource");         System.out.println(dataSource.getDriverClassName());         // 注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean         // factory postprocessor的用户使用ApplicationContext代替BeanFactroy。         ApplicationContext context = new ClassPathXmlApplicationContext(                 "com/starxing/test/conf.xml");         DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context                 .getBean("dataSource");         System.out.println(dataSource2.getDriverClassName());     } } 相关文档:
 使用这一解决方案,我们可以生成如下的属性文件(/WEB-INF/jdbc.properties):
jdbc.driver=org.postgresql.Driver

jdbc.url=jdbc:postgresql://localhost/test

jdbc.user=postgres

jdbc.password=

  我们的Bean配置如下:

<bean id="propertyConfigurer" 

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">

<value>/WEB-INF/jdbc.properties</value>

</property>

</bean>



<bean id="dataSource" 

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName">

<value>${jdbc.driver}</value>

</property>

<property name="url">

<value>${jdbc.url}</value>

</property>

<property name="username">

<value>${jdbc.user}</value>

</property>

<property name="password">

<value>${jdbc.password}</value>

</property>

</bean>

  如上所述,我们定义了一个PropertyPlaceholderConfigurer类的实例,并将其位置属性设置为我们的属性文件。该类被实现为Bean工厂的后处理器,并将使用定义在文件中的属性来代替所有的占位符(${...}value)。

  利用这种技术,我们可以从applicationContext.xml中移除所有特定于主机的配置属性。通过这种方式,我们可以自由地为该文件添加新的Bean,而不必担心特定于主机属性的同步性。这样可以简化生产部署和维护。


PropertyPlaceholderConfigurer作为一个bean factory post-processor实现,可以用来将BeanFactory定义中的属性值放置到另一个单独的Java Properties格式的文件中。这使得用户不用对BeanFactory的主XML定义文件进行复杂和危险的修改,就可以定制一些基本的属性(比如说数据库的urls,用户名和密码)。

考虑一个BeanFactory定义的片断,里面用占位符定义了DataSource:

在下面这个例子中,定义了一个datasource,并且我们会在一个外部Porperties文件中配置一些相关属性。 在运行时,我们为BeanFactory提供一个PropertyPlaceholderConfigurer,它将用Properties文件中的值替换掉这个datasource的属性值:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>

<property name="url"><value>${jdbc.url}</value></property>

<property name="username"><value>${jdbc.username}</value></property>

<property name="password"><value>${jdbc.password}</value></property>

</bean>

真正的值来自于另一个Properties格式的文件:

jdbc.driverClassName=org.hsqldb.jdbcDriver

jdbc.url=jdbc:hsqldb:hsql://production:9002

jdbc.username=sa

jdbc.password=root

如果要在BeanFactory中使用,bean factory post-processor必须手动运行:

XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));

PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

cfg.setLocation(new FileSystemResource("jdbc.properties"));

cfg.postProcessBeanFactory(factory);

注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean factory postprocessor的用户使用ApplicationContext代替BeanFactroy。

PropertyPlaceHolderConfigurer不仅仅在你指定的Porperties文件中查找属性, 如果它在其中没有找到你想使用的属性,它还会在Java的系统properties中查找。 这个行为能够通过设置配置中的systemPropertiesMode 属性来定制。这个属性有三个值, 一个让配置总是覆盖,一个让它永不覆盖,一个让它仅在properties文件中找不到的时候覆盖。 请参考 PropertiesPlaceholderConfigurer的JavaDoc获得更多信息。 文章来源:http://www.cublog.cn/u/9295/showart.php?id=261437

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值