弹簧属性替代测试和生产

Dea*_*ler 26 spring

我在春季碰到了这个房产替代品

<context:property-placeholder location="esb-project-config.properties"/>
Run Code Online (Sandbox Code Playgroud)

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

谢谢,迪恩

tol*_*ius 84

几种方法:


1.'订购'财产

src/main/resources/your-conf.xml

<context:property-placeholder 
         location="classpath:esb-project-config.properties"
         order="1"/>
Run Code Online (Sandbox Code Playgroud)

src/test/resources/your-test-config.xml

<context:property-placeholder 
         location="classpath:esb-project-config.properties"
         order="0"/>
Run Code Online (Sandbox Code Playgroud)

如果您使用src/test/resources测试类路径运行测试,上面将确保src/main/resources/esb-project-config.properties使用src/test/resources/esb-project-config.properties.

这将覆盖整个property-placeholder,因此您必须提供应用程序中所需的所有属性以进行此测试property-placeholder.例如

<context:property-placeholder 
         location="classpath:esb-project-config.properties,
                   classpath:some-other-props-if-needed.properties"
         order="0"/>
Run Code Online (Sandbox Code Playgroud)

2. PropertyOverrideConfigurer

 <context:property-override 
          location="classpath:esb-project-config.test.properties"/>
Run Code Online (Sandbox Code Playgroud)

覆盖某些单独的属性.这里有一些例子


3.系统变量

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

 <context:property-placeholder 
          location="${ENV_SYSTEM:dev}/esb-project-config.properties"/>
Run Code Online (Sandbox Code Playgroud)

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

 <context:property-placeholder 
          location="dev/esb-project-config.properties"/>
Run Code Online (Sandbox Code Playgroud)

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

 <context:property-placeholder 
          location="qa/esb-project-config.properties"/>
Run Code Online (Sandbox Code Playgroud)

4.弹簧轮廓

另一种方法是使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>
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)
  • 注意:"系统变量"和"系统配置文件"方法通常用于在不同的环境之间切换,而不仅仅是在开发模式下"dev <==> test",但仍然是有用的功能需要注意.


Ral*_*lph 8

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

例如:

  • applicationContext.xml - 对于没有任何财产占位符配置的正常配置
  • applicationContext-config.xml - 仅包含加载生产配置文件的property-placeholder.
  • testApplicationContext.xml.该文件includeS上的applicationContext.xml,并使用一个属性占位符与其他属性文件.

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

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


tom*_*tom 5

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

我们使用以下三个功能:

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

classpath:esb-project-config.properties,
classpath:esb-project-config-override.properties
Run Code Online (Sandbox Code Playgroud)

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

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

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

    <context:property-placeholder 
       ignore-resource-not-found="true" ignore-unresolvable="true" 
       location="classpath:esb-project-config.properties,classpath:esb-project-config-override.properties" />
Run Code Online (Sandbox Code Playgroud)