Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring's PropertyPlaceHolderConfigurer won't ignore unresolvable files

Tags:

spring

I am using spring's PropertyPlaceHolderConfigurer as follows :

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="locations">
        <list>
            <value>classpath:default.properties</value>
            <value>file:${user.home}/webextractor.properties</value>
        </list>
    </property>
</bean>

Despite having set the ignoreUnresolvablePlaceholders property to true, I still get a FileNotFoundException on /home/kaykay/webextractor.properties. I know I could just create this file and leave it empty, but I'd like to know what is wrong here.

like image 613
kgautron Avatar asked Oct 07 '12 15:10

kgautron


2 Answers

The ignoreUnresolvablePlaceholders set to true will ignore placeholders that are not set and not throw an exception. For example if you have the following property in your class @Value("${person.age}") and no corresponding value set in your properties file.

The ignoreResourceNotFound property set to true will have the behavior you expected, that is ignore a resource that isn't found.

Hope this helped.

like image 92
AxxA Osiris Avatar answered Nov 20 '22 07:11

AxxA Osiris


I have gone through your problem , I think Osiris is right about the property ignoreUnresolvablePlaceholders . But in case of your , you should to set the property ignoreResourceNotFound true. So that , if the file is not existing then it will ignore that file.

Modified code will be

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:default.properties</value>
            <value>file:${user.home}/webextractor.properties</value>
        </list>
    </property>
</bean>

try this code and let me know.

like image 10
Ashish Avatar answered Nov 20 '22 09:11

Ashish