Spring 弹簧性能替代试验与生产
我在春天遇到这件事是为了财产置换Spring 弹簧性能替代试验与生产,spring,Spring,我在春天遇到这件事是为了财产置换 <context:property-placeholder location="esb-project-config.properties"/> 但不幸的是,我们不希望在xml文件中使用这个文件,因为我们希望在测试中重用该文件,而是将test.properties文件交换给test。我们想要测试所有的产品绑定,但是要使用适合测试的属性,比如localhost。如何加载具有不同属性文件的ApplicationContext 谢谢, 院长 在上下文
<context:property-placeholder location="esb-project-config.properties"/>
但不幸的是,我们不希望在xml文件中使用这个文件,因为我们希望在测试中重用该文件,而是将test.properties文件交换给test。我们想要测试所有的产品绑定,但是要使用适合测试的属性,比如localhost。如何加载具有不同属性文件的ApplicationContext
谢谢,
院长
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" />
几种方法:
1. '订单属性 在
src/main/resources/your-conf.xml
<context:property-placeholder
location="classpath:esb-project-config.properties"
order="1"/>
<context:property-placeholder
location="classpath:esb-project-config.properties"
order="0"/>
如果您使用src/test/resources
作为测试类路径运行测试,则上述操作将确保使用src/test/resources/esb project config.properties
覆盖src/test/resources/esb project config.properties
但是,这将覆盖整个属性占位符
,因此您必须在中为该测试属性占位符
提供应用程序中所需的所有属性。e、 g
<context:property-placeholder
location="classpath:esb-project-config.properties,
classpath:some-other-props-if-needed.properties"
order="0"/>
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>
mvn -Dspring.profiles.active=dev jetty:run
- 注意:“系统变量”和“系统概要文件”方法通常用于在不同的环境之间切换,而不仅仅是在开发模式下进行“开发测试”,但仍然是需要注意的有用功能
<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>
mvn -Dspring.profiles.active=dev jetty:run
——用于不带任何属性占位符配置的正常配置applicationContext.xml
——仅包含加载生产配置文件的属性占位符applicationContext config.xml
。此文件testApplicationContext.xml
包含
s
并将属性占位符与其他属性文件一起使用applicationContext.xml
applicationContext*.xml
对于测试,您只需加载testApplicationContext.xml
,这将包括正常配置,但带有其他属性。似乎:
<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" />
我的首选方法是spring 3.1添加的,如下所示: 在*-context.xml中:
<context:property-placeholder location="classpath:/web-${spring.profiles.active}.properties" />
或者,您可以将参数传递给容器。我尝试了Spring概要文件部分,但它似乎不起作用。我想您不能在内部使用自定义名称空间?您可以正常声明占位符,或者使用:方法4适合我。不过,我并没有在内部使用自定义名称空间。只需使用标准的
上下文:属性占位符
。什么不适合您?我在我的应用程序中完全做到了这一点,没有任何问题。@Ryan我认为第一个不适合我,但我可能错了。如果我记得你不能把这样一个属性占位符标记放在一个bean配置文件标记中。无论如何,在Spring3.1中,最好是在环境中注册PropertySources,并且不要在上下文中声明任何本地属性文件。您使用什么技术来注册PropertySources?初始化器?属性占位符标记应将属性放置在环境中。也就是说,“bean”标记中的属性占位符可能是正确的。我让它工作了,但当我有两个不同的上下文,其中包含属性占位符的bean元素时,它似乎不能正常工作。可能与此错误有关。我使用这样的方法:它允许对属性进行细粒度的控制,并且您还可以在刷新上下文之前将属性添加到Xml应用程序上下文中。
<context:property-placeholder location="classpath:config_${spring.profiles.active}.properties" />
<context:property-placeholder location="classpath:/web-${spring.profiles.active}.properties" />
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>prod</param-value>
</context-param>
mvn -Dspring.profiles.active=dev jetty:run