我有一个在 tomcat 容器中运行的基于 Spring 的 Web 应用程序,我想维护两个不同的配置文件:
- src/main/resources/default.properties:包含开发、集成测试和未设置其他属性时的默认值
- .../tomcat/conf/app.properties:在不同的环境中有不同的内容,应该覆盖default.properties
当应用程序在 tomcat 中运行时,我有一个运行良好的 spring 配置
应用程序上下文.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?