【问题标题】:Duplicate spring properties file重复的弹簧属性文件
【发布时间】:2014-02-06 10:02:50
【问题描述】:

我有两个弹簧属性文件,一个用于集成测试,一个用于实际项目。属性文件大多相同,但在测试属性文件中,我有几个不同的属性用于测试目的。这是一个维护难题,每次我添加一个属性时,我都必须将它复制到测试属性文件中,即使它完全相同。我刚刚在测试中遇到了一个错误,这是由于测试属性文件尚未更新而引起的。

我有一个application-context-test.xml,它从项目application-context.xml 中导入了很多bean,但是覆盖了它出于测试目的需要的bean,我可以对属性文件做同样的事情吗?

这是我的属性文件配置

application-context.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:myproject.properties"/>
</bean>

application-context-test.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:myproject-test.properties"/>
</bean>

【问题讨论】:

    标签: java spring properties


    【解决方案1】:

    您可以创建两个具有不同属性的文件并像这样加载它们:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
                <list>
                   <value="classpath:myproject-test.properties"/>
                   <value="classpath:myproject.properties"/>
                </list>
         </property>
    </bean>
    

    【讨论】:

    • myproject-test.properties 中的属性会覆盖 myproject.properties 中的相同属性吗?
    • 如果再次加载相同的属性,在 java 中加载属性的默认行为会覆盖旧值。所以它也应该这样做。尝试颠倒属性文件的顺序以获得所需的行为。
    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 2012-05-16
    • 2015-12-15
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多