Spring:PropertyPlaceholderConfigurer的使用

PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。这样的话,我只需要对properties文件进行修改,而不用对xml配置文件进行修改。

作用是什么呢?

有一些属性值不需要经常变更,但是有一些属性值可能随时改变,把经常会改动的属性值放在另一个文件中的话,程序使用起来也更方便。

 

PropertyPlaceholderConfigurer可以读取在Properties文件中的配置。

applicationContext.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
  <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
  <list>
   <value>/init.properties</value>
  </list>
 </property>
  </bean>
  <!-- Choose the dialect that matches your "dataSource" definition -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
  destroy-method="close">
  <property name="driverClassName">
   <value>${datasource.driverClassName}</value>
   <!--com.mysql.jdbc.Driver-->
  </property>
  <property name="url">
   <value>${datasource.url}</value>
   <!--jdbc:mysql://localhost:3306/example-->
  </property>
  <property name="username">
   <value>${datasource.username}</value>
   <!--root-->
  </property>
  <property name="password">
   <value>${datasource.password}</value>
   <!--1234-->
  </property>
  <property name="maxActive">
   <value>${datasource.maxActive}</value>
   <!--10-->
  </property>
  <property name="maxIdle">
   <value>${datasource.maxIdle}</value>
   <!--2-->
  </property>
  <property name="maxWait">
   <value>${datasource.maxWait}</value>
   <!--120000-->
  </property>
  <property name="defaultAutoCommit">
   <value>${datasource.defaultAutoCommit}</value>
   <!--true-->
  </property>
 </bean>
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
      <list>
        <value>com/fitech/example/spring/bean/TestBean.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop><!--org.hibernate.dialect.MySQLDialect-->
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop><!--true-->
        <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop><!--50-->
        <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop><!--25-->
      </props>
    </property>
  </bean>
  <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
  <!-- <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    <property name="sessionFactory">
      <ref local="mySessionFactory"/>
    </property>
  </bean> -->
  <bean id="testBeanDao" class="com.fitech.example.spring.dao.hibernate.TestBeanDao">
    <property name="sessionFactory">
      <ref local="mySessionFactory"/>
    </property>
  </bean>
  
  <bean id="caculateService" class="com.fitech.example.spring.business.impl.CaculateImpl">
    <property name="testBeanDao">
      <ref local="testBeanDao"/>
    </property>
  </bean>
  
  <bean id="listService" class="com.fitech.example.spring.business.impl.ListAllImpl">
    <property name="testBeanDao">
      <ref local="testBeanDao"/>
    </property>
  </bean>
</beans>

 

init.properties

datasource.driverClassName=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/example?useUnicode=true&characterEncoding=utf8
datasource.username=root
datasource.password=1234

#datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
#datasource.url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbscs
#datasource.username=sa
#datasource.password=

datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000

#datasource.defaultAutoCommit=true
datasource.defaultAutoCommit=false

datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false

hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
#hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect

hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop

Test.java

Resource res = new ClassPathResource("applicationContext.xml");
  BeanFactory factory = new XmlBeanFactory(res);

  /*不在applicationContext.xml配置PropertyPlaceholderConfigurer 
   * Properties props = new Properties();
   * 
   * props.load(new FileInputStream("init.properties"));
   * PropertyPlaceholderConfigurer cfg = new
   * PropertyPlaceholderConfigurer(); cfg.setProperties(props);
   */

  //配置PropertyPlaceholderConfigurer 
  PropertyPlaceholderConfigurer cfg = (PropertyPlaceholderConfigurer) factory
    .getBean("placeholderConfig");
  cfg.postProcessBeanFactory((XmlBeanFactory) factory);

  IfCaculate caculateService = (IfCaculate) factory
    .getBean("caculateService");
  caculateService.doPlus(123, 321);
  caculateService.doMultiply(123, 321);

  IfListAll listAll = (IfListAll) factory.getBean("listService");
  List all = listAll.listAll();
  for (Iterator iter = all.iterator(); iter.hasNext();) {
   TestBean tb = (TestBean) iter.next();
   System.out.println(tb.getId() + "," + tb.getParam1() + ","
     + tb.getParam2() + "," + tb.getValue());
  }

 

<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:/spring/include/dbQuery.properties</value>
    </property>
</bean>

其中classpath是引用src目录下的文件写法。

 

 

当存在多个Properties文件时,配置就需使用locations了:(2)

 

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
       <list>
          <value>classpath:/spring/include/jdbc-parms.properties</value>
          <value>classpath:/spring/include/base-config.properties</value>
        </list>
    </property>
</bean>

 

接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties 文件,其配置如下:(3)

 

<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="location">
       <value>classpath:/spring/include/dbQuery.properties</value>
    </property>
</bean>

 

<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="2" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="locations">
      <list>
        <value>classpath:/spring/include/jdbc-parms.properties</value>
        <value>classpath:/spring/include/base-config.properties</value>
      </list>
    </property>
</bean>

 

其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true

 

至此你已经了解到了如何使用PropertyPlaceholderConfigurer,如何使用多个Properties文件,以及如何配置多个PropertyPlaceholderConfigurer来分解工程中分散的Properties文件。至于 PropertyPlaceholderConfigurer还有更多的扩展应用,如属性文件加密解密等方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值