21 Haziran 2012 Perşembe

Reloadable message bundles with Spring's ReloadableResourceBundleMessageSource

Spring has a very nice class which saves our lives for reloading .properties files which we use in our applications often.

Say we have a bundle named bundle.properties in our application's classpath and there is a property named sample.property:

sample.property = Matrix Reloaded

We will define a ReloadableResourceBundleMessageSource in our application context for this bundle:


        
        


basename property is the bundle file name and the cacheSeconds property is the duration in seconds which the properties are cached. In the example above 60 seconds is the cache duration, after 60 seconds the message source will be refreshed.

Then in our application we can get a property as:

String sampleProperty = messageSource.getMessage("sample.property", null, null);

Our property can also be parametric. The second null parameter in the function above is the object array for parameters defined. In our sample.property we do not have any parameters so the second parameter is null. And the third parameter is the Locale which the property will be read.