【问题标题】:How to use property file in util:properties?如何在 util:properties 中使用属性文件?
【发布时间】:2014-04-02 11:08:42
【问题描述】:

我必须使用 spring util:properties,但是,我需要这样的东西:

if propertyFile x exists, use x, otherwise, use y.

你能告诉我如何获得这个吗?

【问题讨论】:

    标签: java spring properties


    【解决方案1】:

    也许这是一半的解决方案! Spring 可以加载通配符资源。请看spring <util:properties /> with wildcards

    通过这种方式,您可以将文件命名为:x-config.propertiesy-config.properties

    <bean id = "config" 
        class = "org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name = "locations" 
            value = "classpath*:somefolder/*-config.properties" />
    </bean>
    

    如果 x 和 y 都存在,则它们都被加载。

    【讨论】:

      【解决方案2】:

      其实有一个选项ignoreResourceNotFound,但不适用于命名空间组件。你必须直接使用PropertiesFactoryBean

      <bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="locations" value="y.properties, x.properties"/>
          <property name="ignoreResourceNotFound" value="true"/>
      </bean>
      

      如果您的x.properties 不存在,它将被忽略,y.properties 的属性将保留。

      如果x.properties 存在并且它包含与y.properties 相同的keys,它们会覆盖来自y.properties 的那些,因为所有locations 都是分别加载的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-23
        • 2017-08-01
        • 2022-01-02
        • 2010-10-06
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 2011-12-15
        相关资源
        最近更新 更多