javaspringtomcatconfigurationenvironments

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:

I have a spring configuration that is working fine when the application runs in a tomcat

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:

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?


Solution

  • Since you have a context, where some PP values may be unresolved you can try to use a default variant for the property:

    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, e.g. in the System.getProperties(), which is the case for Tomcat, I guess.

    And after that the ignore-resource-not-found="true" does the stuff.