spring中PropertyPlaceholderConfigurer作用

在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,例如: 

Java代码   
  1. <bean id="propertyConfigurer"    
  2.     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
  3.   <property name="location" value="classpath:config/jdoserver.properties"/>   
  4. </bean>  

但是好像在属性文件定义中却不支持多个属性文件的定义,比如不能这样用config/*.properties。 

经过查看源码,发现可以使用locations属性定义多个配置文件: 

Java代码
  1. <property name="locations">   
  2.             <list>   
  3.                 <value>classpath:config/maxid.properties</value>   
  4.                 <value>classpath:config/jdoserver.properties</value>   
  5.             </list>   
  6. </property> 

使用外部属性后如下: 

Java代码
  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
  2.         <property name="driverClassName" value="${jdbc.agent.driver}"/>   
  3.         <property name="url" value="${jdbc.agent.main.url}"/>   
  4.     </bean>  

例子: 
Spring的框架中为提供了一个BeanFactoryPostProcessor的实体类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 
可以将一些动态参数,移出至.properties中,如此的安排可以让XML定义系统相关设定(不常变动),而.properties可以作为客户根据实际自定义一些相关参数。(如不同人数据库的不同密码) 
applicationConfig.xml 

Java代码
  1. <beans>     
  2.         <bean id="propertyConfigurer"     
  3.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     
  4.         <property name="location">     
  5.             <value>/WEB-INF/db.properties</value>     
  6.         </property>     
  7.     </bean>     
  8.      
  9.     <bean id="dataSource"     
  10.         class="org.logicalcobwebs.proxool.ProxoolDataSource">     
  11.         <property name="driver">     
  12.             <value>${driver}</value>     
  13.         </property>     
  14.         <property name="driverUrl">     
  15.             <value>${driverUrl}</value>     
  16.         </property>     
  17.         <property name="user">     
  18.             <value>${user}</value>     
  19.         </property>     
  20.         <property name="password">     
  21.             <value>${password}</value>     
  22.         </property>     
  23.         <property name="alias">     
  24.             <value>spring</value>     
  25.         </property>     
  26.         <property name="houseKeepingSleepTime">     
  27.             <value>90000</value>     
  28.         </property>     
  29.         <property name="prototypeCount">     
  30.             <value>5</value>     
  31.         </property>     
  32.         <property name="maximumConnectionCount">     
  33.             <value>100</value>     
  34.         </property>     
  35.         <property name="minimumConnectionCount">     
  36.             <value>10</value>     
  37.         </property>     
  38.         <property name="trace">     
  39.             <value>true</value>     
  40.         </property>     
  41.         <property name="verbose">     
  42.             <value>true</value>     
  43.         </property>     
  44.     </bean>     
  45. </beans>    

db.properties 

Java代码
    1. driver=com.mysql.jdbc.Driver      
    2. driverUrl=jdbc:mysql://localhost/securedoc?user=root&password=sqladmin&useUnicode=true&characterEncoding=GBK      
    3. user=root      
    4. password=sqladmin    

posted on 2012-08-05 00:56  站在云端  阅读(605)  评论(0编辑  收藏  举报

导航