简体   繁体   English

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

[英]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应用程序,我想维护两个不同的配置文件:

  • src/main/resources/default.properties : contains defaults for development, integration tests and when no other properties are set src / main / resources / default.properties :包含开发,集成测试以及未设置其他属性的默认值
  • .../tomcat/conf/app.properties: has a different content on different environments and should override default.properties ... / tomcat / conf / app.properties:在不同的环境中有不同的内容,应该覆盖default.properties

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.

相关问题 春天的多种特性。 PropertyPlaceholderConfigurer类中的属性 - multiple properties in spring. property in PropertyPlaceholderConfigurer class Spring:无法使用PropertyPlaceholderConfigurer加载junit上的属性 - Spring: can't load properties on junit with PropertyPlaceholderConfigurer Spring的PropertyPlaceholderConfigurer可以用代码而不是bean读取整个属性文件吗? - Can Spring's PropertyPlaceholderConfigurer read an entire properties file in code, not as a bean? PropertyPlaceholderConfigurer位置内的Spring占位符 - Spring placeholder inside location of PropertyPlaceholderConfigurer 带有Spring PropertyPlaceholderConfigurer属性标志的IllegalArgumentException - IllegalArgumentException with Spring PropertyPlaceholderConfigurer properties flag PropertyPlaceholderConfigurer + PropertiesFactoryBean仅解析位置属性 - PropertyPlaceholderConfigurer + PropertiesFactoryBean only resolving location properties 无法从PropertyPlaceholderConfigurer类Spring中读取属性 - Not able to read properties from PropertyPlaceholderConfigurer class spring 打印通过Spring PropertyPlaceholderConfigurer设置的所有属性 - Printing all properties set via Spring PropertyPlaceholderConfigurer Spring PropertyPlaceholderConfigurer问题加载属性文件 - Spring PropertyPlaceholderConfigurer Issue to Load A Properties File Spring Framework:从 PropertyPlaceholderConfigurer 升级到 PropertyPlaceholderHelper - Spring Framework: upgrade path for PropertyPlaceholderConfigurer to PropertyPlaceholderHelper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM