Sunday, February 7, 2016

Spring reloadable message source

Spring provides you to externalize your messages so that it can be changed without application restart. Add following snippet to your spring configuration.
    <bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>${messages.file}</value>
            </list>
        </property>
        <property name="cacheSeconds" value="1" />
    </bean>

The messages.file is pointing to property loaded by Spring PropertyConfigurer. For development purposes you can keep the messages file bundled with your project in classpath but for real deployment it will be outside.
The value should be like classpath:messages where the messages.properties file is kept in src/main/resources. The value can be messages.file=file:D:/config/messages if its kept out at this location.
The Spring messages are by default internalized so if you have done that setup it can pickup files like messages_en_GB.properties as per the locale.

No comments:

Post a Comment