javaspringproperties-fileproperty-placeholder

spring - <context:property-placeholder> load multiple properties but ignore missing ones


I used the following configuration in my applicationContext.xml

<context:property-placeholder location="classpath:system.properties,file:/data/conf/system.properties,file:/data/conf/1033.properties" ignore-unresolvable="true" />

to load some placeholders:

  1. use these properties defined in classpath:system.properties;
  2. if file or properties exist in /data/conf/system.properties, use them instead of above;
  3. if file or properties exist in /data/conf/1033.properties, use them instead of above.

Now Spring started ok if both /data/conf/system.properties and /data/conf/1033.properties exists, but it will throw rg.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ... if either of them does not exist.

How to tell spring load these properties but ignore missing ones.


Solution

  • You have to add ignore-resource-not-found="true"

    <context:property-placeholder location="classpath:system.properties,file:/data/conf/system.properties,file:/data/conf/1033.properties" 
       ignore-unresolvable="true"  
       ignore-resource-not-found="true"/>