Spring中PropertyPlaceholderConfigurer的使用



Spring中PropertyPlaceholderConfigurer的使用 

    在使用Spring配置獲取properties文件時,在網上查到相關的資料,分享哈!!
(1)獲取一個配置文件
 
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location">
             <value>file:./mes.properties</value>
        </property>  
</bean>
 

其中classpath是引用src目錄下的文件寫法。

(2)獲取多個配置文件時,配置就需要使用locations

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
             <value>classpath:/resources/jdbc.properties</value>
            <value>classpath:/resources/config.properties</value>        
       </property>  
</bean>

(3)使用多個PropertyPlaceholderConfigurer來分散配置,達到整合多工程下的多個分散的Properties 文件,其配置如下: 
   

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

<property name="order" value="1" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="location">
       <value>classpath:classpath:/resources/mes.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:/resources/jdbc.properties</value>
        <value>classpath:/resources/config.properties</value>
      </list>
    </property>
</bean> 

其中order屬性代表其加載順序,而ignoreUnresolvablePlaceholders爲是否忽略不可解析的 Placeholder,如配置了多個PropertyPlaceholderConfigurer,則需設置爲true

PropertyPlaceholderConfigurer還有更多的擴展應用,如屬性文件加密解密等方法


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章