spring-framework / org.springframework.context.support / ReloadableResourceBundleMessageSource

ReloadableResourceBundleMessageSource

open class ReloadableResourceBundleMessageSource : AbstractResourceBasedMessageSource, ResourceLoaderAware

Spring-specific org.springframework.context.MessageSource implementation that accesses resource bundles using specified basenames, participating in the Spring org.springframework.context.ApplicationContext's resource loading.

In contrast to the JDK-based ResourceBundleMessageSource, this class uses java.util.Properties instances as its custom data structure for messages, loading them via a org.springframework.util.PropertiesPersister strategy from Spring Resource handles. This strategy is not only capable of reloading files based on timestamp changes, but also of loading properties files with a specific character encoding. It will detect XML property files as well.

Note that the basenames set as "basenames" property are treated in a slightly different fashion than the "basenames" property of 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) might not work reliably in this case.

For a typical web application, message files could be placed in WEB-INF: e.g. a "WEB-INF/messages" basename would find a "WEB-INF/messages.properties", "WEB-INF/messages_en.properties" etc arrangement as well as "WEB-INF/messages.xml", "WEB-INF/messages_en.xml" etc. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to sequential lookup.

This MessageSource can easily be used outside of an org.springframework.context.ApplicationContext: it will use a 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 Also
#setCacheSeconds#setBasenames#setDefaultEncoding#setFileEncodings#setPropertiesPersister#setResourceLoaderorg.springframework.util.DefaultPropertiesPersisterorg.springframework.core.io.DefaultResourceLoaderResourceBundleMessageSourcejava.util.ResourceBundle

Constructors

<init>

ReloadableResourceBundleMessageSource()

Spring-specific org.springframework.context.MessageSource implementation that accesses resource bundles using specified basenames, participating in the Spring org.springframework.context.ApplicationContext's resource loading.

In contrast to the JDK-based ResourceBundleMessageSource, this class uses java.util.Properties instances as its custom data structure for messages, loading them via a org.springframework.util.PropertiesPersister strategy from Spring Resource handles. This strategy is not only capable of reloading files based on timestamp changes, but also of loading properties files with a specific character encoding. It will detect XML property files as well.

Note that the basenames set as "basenames" property are treated in a slightly different fashion than the "basenames" property of 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) might not work reliably in this case.

For a typical web application, message files could be placed in WEB-INF: e.g. a "WEB-INF/messages" basename would find a "WEB-INF/messages.properties", "WEB-INF/messages_en.properties" etc arrangement as well as "WEB-INF/messages.xml", "WEB-INF/messages_en.xml" etc. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to sequential lookup.

This MessageSource can easily be used outside of an org.springframework.context.ApplicationContext: it will use a 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!

Functions

clearCache

open fun clearCache(): Unit

Clear the resource bundle cache. Subsequent resolve calls will lead to reloading of the properties files.

clearCacheIncludingAncestors

open fun clearCacheIncludingAncestors(): Unit

Clear the resource bundle caches of this MessageSource and all its ancestors.

setConcurrentRefresh

open fun setConcurrentRefresh(concurrentRefresh: Boolean): Unit

Specify whether to allow for concurrent refresh behavior, i.e. one thread locked in a refresh attempt for a specific cached properties file whereas other threads keep returning the old properties for the time being, until the refresh attempt has completed.

Default is "true": this behavior is new as of Spring Framework 4.1, minimizing contention between threads. If you prefer the old behavior, i.e. to fully block on refresh, switch this flag to "false".

setFileEncodings

open fun setFileEncodings(fileEncodings: Properties): Unit

Set per-file charsets to use for parsing properties files.

Only applies to classic properties files, not to XML files.

setPropertiesPersister

open fun setPropertiesPersister(propertiesPersister: PropertiesPersister): Unit

Set the PropertiesPersister to use for parsing properties files.

The default is a DefaultPropertiesPersister.

setResourceLoader

open fun setResourceLoader(resourceLoader: ResourceLoader): Unit

Set the ResourceLoader to use for loading bundle properties files.

The default is a DefaultResourceLoader. Will get overridden by the ApplicationContext if running in a context, as it implements the ResourceLoaderAware interface. Can be manually overridden when running outside of an ApplicationContext.

toString

open fun toString(): String