Search code examples
springproperty-files

Context vs context - setting ignore-unresolvable="true" - PropertyPlaceholderConfigurer


Problem

I'm trying to set ignore-unresolvable="true".

I have found the answer https://stackoverflow.com/a/11773267/1688441 from the question how to define not mandatory property in spring? .

The example they show is:

<context:property-placeholder ignore-unresolvable="true" ... />

However, in the project I have inherited we have a project file called project.xml that contains Resource definitions with a Context tag.

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource />
<ResourceLink />
<Resource />
</Context>

Note: The resources have been removed

When I edit the Context tag to add ignore-resolvable everything breaks and not even my DataSource resource is read. Anyone have any ideas?

I tried the following:

<Context:property-placeholder ignore-unresolvable="true">

Possibly related:

spring PropertyPlaceholderConfigurer and context:property-placeholder


Solution

  • It turns out that in the specific project a class based configuration was being used instead of XML. I found the following class to which I added setIgnoreUnresolvablePlaceholders(false) in the method that returns PropertySourcesPlaceholderConfigurer:

    @Configuration
    @ComponentScan
    @EnableWebMvc
    @EnableAsync
    @EnableScheduling
    @PropertySource(value = {"classpath:appProp.properties"})
    @Import({ExternalizeConfiguration.class, AppApplication.class,
            AppPersistenceApplication.class, ConnectBoIntegrationApplication.class})
    public class AppWebApplication extends WebMvcConfigurerAdapter {
    
    ...Other Code...
    
    /**
         * Bean required for Value annotation
         */
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            PropertySourcesPlaceholderConfigurer test = new PropertySourcesPlaceholderConfigurer();
            test.setIgnoreUnresolvablePlaceholders(false);
            return test;
        }
    
    }
    

    So my understanding is that annotating this method as @Bean causes the method to execute whenever an object of type PropertySourcesPlaceholderConfigurer is auto-wired. In this way, we control which instance is used and what params are set on it.