弹簧属性替代测试和生产

时间:2012-02-27 18:54:33

标签: spring

我在春天遇到了这个属性替换

<context:property-placeholder location="esb-project-config.properties"/>

但遗憾的是,我们不希望在xml文件中使用此文件,因为我们希望在测试中重用该文件,而是交换test.properties文件进行测试。即。我们想要测试所有生产绑定,但是要使用适合像localhost这样的测试的属性。我们如何加载ApplicationContext但使用不同的属性文件?

感谢, 迪安

5 个答案:

答案 0 :(得分:84)

几种方法:


1。 '订单'属性

src/main/resources/your-conf.xml

中的

<context:property-placeholder 
         location="classpath:esb-project-config.properties"
         order="1"/>
src/test/resources/your-test-config.xml

中的

<context:property-placeholder 
         location="classpath:esb-project-config.properties"
         order="0"/>

如果您使用src/test/resources作为测试类路径运行测试,则上述内容将确保使用src/main/resources/esb-project-config.properties覆盖src/test/resources/esb-project-config.properties

这会覆盖整个property-placeholder,因此您必须提供应用程序中所需的所有属性才能进行此测试property-placeholder。 e.g。

<context:property-placeholder 
         location="classpath:esb-project-config.properties,
                   classpath:some-other-props-if-needed.properties"
         order="0"/>

2。 PropertyOverrideConfigurer

 <context:property-override 
          location="classpath:esb-project-config.test.properties"/>

覆盖某些个人属性。一些例子here


3。系统变量

您可以使用前缀来控制特定于环境的属性,这可以通过使用系统变量来完成:

 <context:property-placeholder 
          location="${ENV_SYSTEM:dev}/esb-project-config.properties"/>

在这种情况下,它总是在下面看:

 <context:property-placeholder 
          location="dev/esb-project-config.properties"/>

默认情况下,除非设置了ENV_SYSTEM系统变量。例如,如果它设置为qa,它将自动显示在:

 <context:property-placeholder 
          location="qa/esb-project-config.properties"/>

4。 Spring Profiles

另一种方法是使bean配置文件具体化。例如:

<beans profile="dev">
  <context:property-placeholder 
           location="esb-project-config.dev.properties"/>
</beans>

<beans profile="qa">
  <context:property-placeholder 
           location="esb-project-config.qa.properties"/>
</beans>

将根据个人资料集加载相应的esb-project-config。例如,这将加载esb-project-config.dev.properties

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles( "dev" );
ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
ctx.refresh();

  • 注意:“系统变量”和“系统配置文件”方法通常用于在开发模式下切换不同的环境而不仅仅是“dev&lt; ==&gt; test”,但仍然是有用的功能需要注意。

答案 1 :(得分:8)

将property-placeholder配置放在一个额外的spring xml配置文件中。

例如:

  • applicationContext.xml - 对于没有任何财产占位符配置的正常配置
  • applicationContext-config.xml - 仅包含加载生产配置文件的属性占位符。
  • testApplicationContext.xml。此文件includeapplicationContext.xml,并使用属性占位符和其他属性文件。

在Web App中,您可以使用此模式applicationContext*.xml加载所有生产弹簧上下文文件。

对于测试,您只需加载testApplicationContext.xml,这将包括普通配置,但包含其他属性。

答案 2 :(得分:5)

  • 在上下文标记上,您可以指示属性文件是否存在 不存在它不需要失败。
  • 属性文件按声明的顺序加载。 (这个 也可能是在标签上声明的属性。不确定)
  • 如果多次声明属性,则最后加载的值为 使用。

我们使用以下三个功能:

我们声明了两个属性文件:

classpath:esb-project-config.properties,
classpath:esb-project-config-override.properties

第一个属性文件包含合理的默认值和开发配置。此文件是您的应用程序的一部分。

第二个属性文件是测试类路径或应用程序服务器的生产类路径上可用的文件。此文件是应用程序的外部 这样我们就可以覆盖每个环境的属性,并且只有一个版本的应用程序。

以下是我们使用的属性示例:

    <context:property-placeholder 
       ignore-resource-not-found="true" ignore-unresolvable="true" 
       location="classpath:esb-project-config.properties,classpath:esb-project-config-override.properties" />

答案 3 :(得分:4)

我在首选3.1中添加的首选方法如下:

在* -context.xml中:

<context:property-placeholder location="classpath:/web-${spring.profiles.active}.properties" />

并在web.xml中:

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>prod</param-value>
</context-param>

然后,您可以在运行时指定环境,例如:

mvn -Dspring.profiles.active=dev jetty:run

或者你将参数传递给容器。

答案 4 :(得分:3)

似乎:

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

它不起作用。但你可以这样做:

<context:property-placeholder location="classpath:config_${spring.profiles.active}.properties" />
相关问题