2014-02-06 117 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