【问题标题】:Overriding property file using spring使用spring覆盖属性文件
【发布时间】:2012-09-05 02:42:45
【问题描述】:

我在我的一个 Spring (3.1) XML 中定义了以下属性文件:

<context:property-placeholder location="classpath:MyConfigFile.properties"/> 

我希望能够定义第二个 可选 属性文件,该文件将覆盖“MyConfigFile.properties”文件并被加载而不是它。

换句话说,我希望我的应用程序加载“MyConfigFile.properties”文件,但如果“StrogerConfigFile.properties”在类路径中可用,它将被加载。

有人知道如何使用 Spring XML 来完成吗?

【问题讨论】:

    标签: xml spring javabeans placeholder


    【解决方案1】:
    <context:property-placeholder location="file:///[path]/override1.properties, file:///[path]/override2.properties" properties-ref="defaultProps" />
    
    
    <bean id="defaultProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <array>
                <value>classpath:default1.properties</value>
                <value>classpath:default2.properties</value>
            </array>
        </property>
        <property name="properties">
            <util:properties local-override="true">
                <prop key="some.property">some value</prop>
            </util:properties>
        </property>
    </bean>
    

    这是我使用的非常灵活的设置。允许您直接在 xml 中使用基本默认值,在属性文件中使用默认值并在另一个属性文件中覆盖。

    【讨论】:

      【解决方案2】:

      你试过了吗

      <property name="ignoreResourceNotFound" value="true"/>
      <property name="locations">
          <list>
              <value>classpath:default.properties</value>
              <value>classpath:overwrite.properties</value>
          </list>
      </property>
      

      【讨论】:

      • 你错过了包含 bean 标记
      猜你喜欢
      • 2012-07-23
      • 2016-08-07
      • 2013-08-16
      • 2016-05-15
      • 2014-01-27
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多