【发布时间】:2015-03-07 19:14:46
【问题描述】:
我有一个在 tomcat 容器中运行的基于 Spring 的 Web 应用程序,我想维护两个不同的配置文件:
- src/main/resources/default.properties:包含开发、集成测试以及未设置其他属性时的默认值
- .../tomcat/conf/app.properties:在不同的环境下有不同的内容,应该覆盖default.properties
当应用程序在 tomcat 中运行时,我有一个运行良好的 spring 配置
app-context.xml
<context:property-placeholder
ignore-resource-not-found="true"
ignore-unresolvable="true"
system-properties-mode="OVERRIDE"
location="classpath:default.properties,
file:${catalina.home}/conf/app.properties"/>
但是当我尝试在集成测试中使用此配置时,在 tomcat 容器之外,加载 applicationContext 失败:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
...
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0' defined in null: Could not resolve placeholder 'catalina.home'
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:287)
当当前上下文中没有设置属性 catalina.home 时,有什么方法可以告诉 Spring 忽略位置文件:${catalina.home}/conf/app.properties?
【问题讨论】:
-
试试
PP default value:file:${catalina.home:}/conf/app.properties -
@ArtemBilan 确实成功了,但我无法将您的评论标记为答案。您可以将其发布为答案吗?
-
完成。感谢您的确认!我不确定,beucase 从来没有满足过类似的要求 :-)。
标签: java spring tomcat configuration environments