2014-02-06 76 views
0

我有两个弹簧属性文件,一个用于集成测试,另一个用于实际项目。属性文件大部分是相同的,但在测试属性文件中,我有几个属性在测试目的上是不同的。这是一个维护痛苦,每次我添加一个属性,我必须将它复制到测试属性文件中,即使它完全一样。我在测试中遇到了一个错误,这是由于测试属性文件未被更新而导致的。重复弹簧属性文件

我有一个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> 

回答

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> 
+0

myproject-test.properties中的属性是否会覆盖myproject.properties中的相同属性? – user86834

+0

如果再次加载相同的属性,则在java中加载属性的默认行为将覆盖旧值。所以它也应该这样做。尝试颠倒属性文件的顺序以获得所需的行为。 – Sanjeev