spring ignore-resource-not-found vs ignore-unresolvable and default value

The configration metadata customzation detail can be found HERE

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 key/value set in your properties file.

The ignoreResourceNotFound property set to true will  ignore a resource(file or dababase) that isn’t found.

example for set default value :

1. Set the default value separator in config

<bean>
    <property name="location" value="classpath:application.properties" />
    <-- Ignore file not found -->
    <property name="ignoreResourceNotFound" value="true" /> 
    <-- Ignore when tokens are not found -->
    <property name="ignoreUnresolvablePlaceholders" value="true" />

    <!-- Token that separates default values on a placeholder-by-placeholder 
            basis e.g. ${server.name?localhost} -->
    <property name="valueSeparator" value="?" />
            ...
</bean>

2. Set the default values for your properties

<bean id="myServer">
    <property name="serverName" value="${server.name?localhost}" />
    <property name="serverPort" value="${server.port?25}" />
            ...
</bean>

Notes

  • If unspecified, the default separator is a colon “:”
  • This option is not available when using the “context” namespace – (submitted Jira ticket: SPR-7794)

Another way, we can keep our defaults in one place instead of having to look for them in all the files.

    <bean id="my_propertyPlaceholderConfigurer"
         >
        <property name="ignoreResourceNotFound" value="true" />

        <property name="locations">
            <list>
                <value>classpath:my-default.properties</value>
                <value>classpath:my.properties</value>
            </list>
        </property>
    </bean>

Another way:

if a properties entry is missing from properties file, you will get BeanDefinitionStoreException. This is reasonable in most of time. But sometime you will just want to set some default value for certain entry (i.e. new entry that only applicable for new version and you want it to be backward compatible with old version configuration file).

For those scenarios, default value is supported by PropertyPlaceholderConfigurer as well by its setProperties(Properties props)method. Below is how it will look like:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>my_config.properties</value></property>
<property name="properties">
<props>
<prop key="entry.1">123</prop>
</props>
</property>
</bean>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s