gpt4 book ai didi

java - Spring PropertyPlaceholderConfigurer 可以忽略属性位置路径中未设置的属性吗?

转载 作者:搜寻专家 更新时间:2023-11-01 03:51:05 25 4
gpt4 key购买 nike

我有一个在 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 值可能无法解析,您可以尝试为该属性使用默认变体:

file:${catalina.home:}/conf/app.properties

在这种情况下,如果没有 catalina.home 属性,路径前缀将解析为空字符串,例如在 System.getProperties() 中,我猜 Tomcat 就是这种情况。

然后 ignore-resource-not-found="true" 完成这些工作。

关于java - Spring PropertyPlaceholderConfigurer 可以忽略属性位置路径中未设置的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27861035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com