2012-11-27 27 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

谢谢!春季配置文件对我来说可以... –