spring 测试和生产的弹簧属性替代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9470551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:58:35  来源:igfitidea点击:

spring property substitution for test and production

spring

提问by Dean Hiller

I ran into this for property substitution in spring

我在春天遇到了这个财产替换

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

but unfortunately, we don't want this in the xml file as we want to re-use the file in our tests but swap in the test.properties file for test. ie. we want to test all the production bindings but with properties that are suitable for test like localhost for instance. How can we load the ApplicationContext but with different properties files?

但不幸的是,我们不希望在 xml 文件中使用它,因为我们想在测试中重新使用该文件,但要在 test.properties 文件中交换以进行测试。IE。我们想要测试所有生产绑定,但具有适合测试的属性,例如 localhost。我们如何加载 ApplicationContext 但具有不同的属性文件?

thanks, Dean

谢谢,院长

采纳答案by Ralph

Put the property-placeholder configuration in an extra spring xml configuration file.

将属性占位符配置放在一个额外的 spring xml 配置文件中。

For example:

例如:

  • applicationContext.xml-- for the normal configration without any property-placeholder configuration
  • applicationContext-config.xml-- contains only a property-placeholder that load the production config file.
  • testApplicationContext.xml. This file includes the applicationContext.xmland uses a property-placeholder with an other properties file.
  • applicationContext.xml-- 对于没有任何属性占位符配置的正常配置
  • applicationContext-config.xml-- 仅包含加载生产配置文件的属性占位符。
  • testApplicationContext.xml. 该文件includeS上的applicationContext.xml,并使用一个属性占位符与其他属性文件。

In a Web App you could load all production spring context files with this pattern applicationContext*.xml.

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

For the tests you need only to load testApplicationContext.xmlthis will include the normal config, but with other properties.

对于测试,您只需要加载testApplicationContext.xml这将包括正常配置,但具有其他属性。

回答by tolitius

several approaches:

几种方法:



1. 'Order' Property

1.“订单”属性

in src/main/resources/your-conf.xml

src/main/resources/your-conf.xml

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

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

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

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

If you running your test with src/test/resourcesas a test classpath, the above will ensure to override src/main/resources/esb-project-config.propertieswith the src/test/resources/esb-project-config.properties.

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

This will override the whole property-placeholderthough, so you would have to provide all the properties needed in your application in for this test property-placeholder. e.g.

property-placeholder不过,这将覆盖整个内容,因此您必须为此测试提供应用程序所需的所有属性property-placeholder。例如

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


2. PropertyOverrideConfigurer

2. 属性覆盖配置器

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

to override certain individualproperties. Some examples here

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



3. System Variables

3. 系统变量

You can use a prefix to control environment specific properties, this can be done by using system variables:

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

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

In this case it will always look under:

在这种情况下,它将始终查看:

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

by default, unless a ENV_SYSTEMsystem variable is set. If it is set to qa, for example, it will automatically look under:

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

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


4. Spring Profiles

4. 弹簧配置文件

Another approach is to make beans profile specific. For example:

另一种方法是使 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>

The appropriate esb-project-configwill loaded depending on a profile set. For example this will load esb-project-config.dev.properties:

适当的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();


  • NOTE: "System Variables" and "System Profiles" approaches are usually used to switch between different environmentsrather than just "dev <==> test" in dev mode, but still are useful capabilities to be aware of.
  • 注意:“系统变量”和“系统配置文件”方法通常用于在不同环境之间切换,而不仅仅是开发模式下的“开发 <==> 测试”,但仍然是需要注意的有用功能。

回答by tom

  • On the context tag you can indicate that if a properties file does not exist it does not need to fail.
  • Property files are loaded in the order that they are declared. (This might also be a property to declare on the tag. Not sure)
  • If a property is declared multiple times, the last loaded value is used.
  • 在上下文标记上,您可以指示如果属性文件不存在,则不需要失败。
  • 属性文件按照它们的声明顺序加载。(这也可能是要在标签上声明的属性。不确定)
  • 如果一个属性被多次声明,则使用最后加载的值。

We use these three features as follows:

我们使用这三个功能如下:

We declare two property files:

我们声明两个属性文件:

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

The first property file contains sensible defaults and development configuration. This file is part of your application.

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

The second property file is a file that is available on the test classpath or even the production classpath of the application server. This file is externalof the application That way we can override properties for each environment and have just one version of our application.

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

So here is the example of the properties we use:

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

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

回答by Vetsin

My preferred method, as added by spring 3.1, is as follows:

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

In your *-context.xml:

在您的 *-context.xml 中:

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

and in web.xml:

在 web.xml 中:

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

You can then specify the environment at runtime, for example:

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

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

Or however you pass arguments to your container.

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

回答by Sebastien Lorber

It seems:

它似乎:

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

It doesn't work. But you can do:

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

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