[英]Can Spring PropertyPlaceholderConfigurer ignore unset properties in a property location path?
I have a Spring-based web application running in a tomcat container and I want to maintain two different configuration files: 我有一个在tomcat容器中运行的基于Spring的Web应用程序,我想维护两个不同的配置文件:
I have a spring configuration that is working fine when the application runs in a tomcat 当应用程序在tomcat中运行时,我有一个正常工作的spring配置
app-context.xml 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"/>
But when I try to use this configuration in an integration test, outside of a tomcat container, loading the applicationContext fails: 但是当我尝试在集成测试中使用此配置时,在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)
Is there any way to tell Spring to ignore the location file:${catalina.home}/conf/app.properties when the property catalina.home is not set in the current context? 有没有办法告诉Spring忽略位置文件:$ {catalina.home} /conf/app.properties什么时候在当前上下文中没有设置属性catalina.home?
you can set ignore-resource-not-found
to solve this problem. 你可以设置
ignore-resource-not-found
来解决这个问题。
Try this: 尝试这个:
<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"/>
Code snippet from spring-context.xsd: spring-context.xsd的代码片段:
<xsd:attribute name="ignore-resource-not-found" type="xsd:boolean" default="false">
<xsd:annotation>
<xsd:documentation><![CDATA[
Specifies if failure to find the property resource location should be ignored.
Default is "false", meaning that if there is no file in the location specified
an exception will be raised at runtime.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
Since you have a context, where some PP values may be unresolved you can try to use a default variant
for the property: 由于您有一个上下文,其中某些PP值可能未解析,您可以尝试使用该属性的
default variant
:
file:${catalina.home:}/conf/app.properties
In this case the path prefix is resolved to the empty String if there is no catalina.home
property, eg in the System.getProperties()
, which is the case for Tomcat, I guess. 在这种情况下,如果没有
catalina.home
属性,则路径前缀被解析为空String,例如在System.getProperties()
,我猜这是Tomcat的情况。
And after that the ignore-resource-not-found="true"
does the stuff. 之后,
ignore-resource-not-found="true"
完成了这些工作。
What I'd suggest is using spring profiles to enable/disable property files. 我建议使用弹簧配置文件来启用/禁用属性文件。
When running in tomcat swith on the "tomcat" or "web" profile and put the loading of the app.properties into that profile. 在tomcat上运行“tomcat”或“web”配置文件时,将app.properties加载到该配置文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.