In contrast to the JDK-based {@link ResourceBundleMessageSource}, this class uses {@link java.util.Properties} instances as its custom data structure for messages,loading them via a {@link org.springframework.util.PropertiesPersister} strategyfrom Spring {@link Resource} handles. This strategy is not only capable ofreloading files based on timestamp changes, but also of loading properties files with a specific character encoding. It will detect XML property files as well.
In contrast to {@link ResourceBundleMessageSource}, this class supports reloading of properties files through the {@link #setCacheSeconds "cacheSeconds"}setting, and also through programmatically clearing the properties cache. Since application servers typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath will not be reflected in the application.
Note that the base names set as {@link #setBasenames "basenames"} propertyare treated in a slightly different fashion than the "basenames" property of {@link ResourceBundleMessageSource}. It follows the basic ResourceBundle rule of not specifying file extension or language codes, but can refer to any Spring resource location (instead of being restricted to classpath resources). With a "classpath:" prefix, resources can still be loaded from the classpath, but "cacheSeconds" values other than "-1" (caching forever) will not work in this case.
This MessageSource implementation is usually slightly faster than {@link ResourceBundleMessageSource}, which builds on {@link java.util.ResourceBundle}- in the default mode, i.e. when caching forever. With "cacheSeconds" set to 1, message lookup takes about twice as long - with the benefit that changes in individual properties files are detected with a maximum delay of 1 second. Higher "cacheSeconds" values usually do not make a significant difference.
This MessageSource can easily be used outside of an {@link org.springframework.context.ApplicationContext}: It will use a {@link org.springframework.core.io.DefaultResourceLoader} as default,simply getting overridden with the ApplicationContext's resource loader if running in a context. It does not have any other specific dependencies.
Thanks to Thomas Achleitner for providing the initial implementation of this message source! @author Juergen Hoeller @see #setCacheSeconds @see #setBasenames @see #setDefaultEncoding @see #setFileEncodings @see #setPropertiesPersister @see #setResourceLoader @see org.springframework.util.DefaultPropertiesPersister @see org.springframework.core.io.DefaultResourceLoader @see ResourceBundleMessageSource @see java.util.ResourceBundle
|
|
|
|
|
|