spring propertyplaceholderconfigurer 讲解

来源:互联网 发布:淘宝前100名半价怎么抢 编辑:程序博客网 时间:2024/04/30 03:38

Spring的框架中为您提供了一个BeanFactoryPostProcessor的实体类别 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,

基于这个类别,您可以将一些组件设定,移出至.properties档案中,如此的安排可以让xml定义档负责系统关设定,而.properties档可以作为客户根据

需要,自定义一些相关的参

在使用Spring配置获取properties文件时,

下面实例参数

1:获取一个配置文件

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

     <property name="location">

                     <value>classpath:/resourece/jdbc.properties</value>

    </property>

</bean>

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

2:获取多个配置文件时,配置就需要使用locations

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

    <property name="locations">

        <value>classpath:/com/.jdbc.properties</value>

         <value>classpath:/com/config.properties</value>

      <property>

</bean>
3:使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下

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

   <property name="order" value="1"/>

   <property name="localtion">

          <value>classpath:/com/mes.properties</value>

    </property>

</bean>


<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PorpertyPlaceholderConfigurer">

        <property name="order" value="2"/>

         <property name="ignoreUnresolvablePlaceholders" value="true"/>

          <property name="locations">

              <list>

                    <value>classpath:/com/jdbc.properties</value>                  
                   <value>classpath:/com/config.properties</value>

             </list>

<property>

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

PorpertyPlaceholderConfigurer还有多的扩展应用,如属性文件加密解密等方法





0 0