2012-11-27 29 views
2

我有非標準的maven文件夾結構:春性能優先

src/main/java 
src/main/resources 
src/test/java 
src/test/resources 

我appicationContext包含以下內容:

<!-- load properties files --> 
<context:property-placeholder location="classpath*:*.properties"/> 

我已經定義了2個hibernate.properties文件 - 一個用於src/main/resources,一個用於src/ test/resources。我曾預計,當我將運行測試時,我的測試hibernate.properties將覆蓋生產hibernate.properties。而不是這兩個文件加載和生產版本使用:

Loading properties file from file [D:\projects\video_crawler_v3\out\test\core\hibernate.properties] 
Loading properties file from file [D:\projects\video_crawler_v3\out\production\core\hibernate.properties] 

如何正確設置我的屬性文件?我使用IntelliJ IDEA的編譯和運行測試

+0

你可以添加一個'hibernate.properties'文件的例子嗎?我只是想看看你的主配置和測試配置之間的差異。謝謝。 – Jonathan

+0

我不認爲命名屬性佔位符配置文件'hibernate.properties'是一個好主意,'hibernate.properties'通常是Hibernate自身的配置文件,並且與Spring屬性佔位符無關。 –

回答

2

其中一個選項是春天配置文件在你的context.xml http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/

把兩個「屬性」版本,例如:需要

<beans> 

    ... your beans 

    <beans profile="prod"> 
     <context:property-placeholder location="classpath:/hibernate.properties" /> 
    </beans> 

    <beans profile="test"> 
     <context:property-placeholder location="classpath:/test-hibernate.properties" /> 
    </beans> 
</beans> 

激活配置文件與-Dspring.profiles.active = test。

注:使用www.springframework.org/schema/beans/spring-beans-3.1.xsd

+0

您應該演示Spring Profiles如何成爲解決方案。另外需要注意的是:該功能僅適用於Spring 3.1及更高版本。 – Jonathan

+0

如果您使用的是TextContext框架,您可以使用'@ActiveProfiles(「test」)'而不是'-Dspring.profiles.active = test'框架http://static.springsource.org/spring/docs/current/spring-framework -reference/html/testing.html –

+0

謝謝!春季配置文件對我來說可以... –