public class ReloadableResourceBundleMessageSource extends AbstractMessageSource implements ResourceLoaderAware
MessageSource
implementation
that accesses resource bundles using specified basenames, participating in the
Spring ApplicationContext
's resource loading.
In contrast to the JDK-based ResourceBundleMessageSource
, this class uses
Properties
instances as its custom data structure for messages,
loading them via a 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.
In contrast to ResourceBundleMessageSource
, this class supports
reloading of properties files through the "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 "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) will not work in this case.
This MessageSource implementation is usually slightly faster than
ResourceBundleMessageSource
, which builds on 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
ApplicationContext
: It will use a
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!
setCacheSeconds(int)
,
setBasenames(java.lang.String...)
,
setDefaultEncoding(java.lang.String)
,
setFileEncodings(java.util.Properties)
,
setPropertiesPersister(org.springframework.util.PropertiesPersister)
,
setResourceLoader(org.springframework.core.io.ResourceLoader)
,
DefaultPropertiesPersister
,
DefaultResourceLoader
,
ResourceBundleMessageSource
,
ResourceBundle
Modifier and Type | Class and Description |
---|---|
protected class |
ReloadableResourceBundleMessageSource.PropertiesHolder
PropertiesHolder for caching.
|
logger
Constructor and Description |
---|
ReloadableResourceBundleMessageSource() |
Modifier and Type | Method and Description |
---|---|
protected List<String> |
calculateAllFilenames(String basename,
Locale locale)
Calculate all filenames for the given bundle basename and Locale.
|
protected List<String> |
calculateFilenamesForLocale(String basename,
Locale locale)
Calculate the filenames for the given bundle basename and Locale,
appending language code, country code, and variant code.
|
void |
clearCache()
Clear the resource bundle cache.
|
void |
clearCacheIncludingAncestors()
Clear the resource bundle caches of this MessageSource and all its ancestors.
|
protected ReloadableResourceBundleMessageSource.PropertiesHolder |
getMergedProperties(Locale locale)
Get a PropertiesHolder that contains the actually visible properties
for a Locale, after merging all specified resource bundles.
|
protected ReloadableResourceBundleMessageSource.PropertiesHolder |
getProperties(String filename)
Get a PropertiesHolder for the given filename, either from the
cache or freshly loaded.
|
protected Properties |
loadProperties(Resource resource,
String filename)
Load the properties from the given resource.
|
protected ReloadableResourceBundleMessageSource.PropertiesHolder |
refreshProperties(String filename,
ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
Refresh the PropertiesHolder for the given bundle filename.
|
protected MessageFormat |
resolveCode(String code,
Locale locale)
Resolves the given message code as key in the retrieved bundle files,
using a cached MessageFormat instance per message code.
|
protected String |
resolveCodeWithoutArguments(String code,
Locale locale)
Resolves the given message code as key in the retrieved bundle files,
returning the value found in the bundle as-is (without MessageFormat parsing).
|
void |
setBasename(String basename)
Set a single basename, following the basic ResourceBundle convention of
not specifying file extension or language codes, but in contrast to
ResourceBundleMessageSource referring to a Spring resource location:
e.g. |
void |
setBasenames(String... basenames)
Set an array of basenames, each following the basic ResourceBundle convention
of not specifying file extension or language codes, but in contrast to
ResourceBundleMessageSource referring to a Spring resource location:
e.g. |
void |
setCacheSeconds(int cacheSeconds)
Set the number of seconds to cache loaded properties files.
|
void |
setConcurrentRefresh(boolean concurrentRefresh)
Specify whether to allow for concurrent refresh behavior, i.e.
|
void |
setDefaultEncoding(String defaultEncoding)
Set the default charset to use for parsing properties files.
|
void |
setFallbackToSystemLocale(boolean fallbackToSystemLocale)
Set whether to fall back to the system Locale if no files for a specific
Locale have been found.
|
void |
setFileEncodings(Properties fileEncodings)
Set per-file charsets to use for parsing properties files.
|
void |
setPropertiesPersister(PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files.
|
void |
setResourceLoader(ResourceLoader resourceLoader)
Set the ResourceLoader to use for loading bundle properties files.
|
String |
toString() |
getCommonMessages, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage
createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat
public ReloadableResourceBundleMessageSource()
public void setBasename(String basename)
ResourceBundleMessageSource
referring to a Spring resource location:
e.g. "WEB-INF/messages" for "WEB-INF/messages.properties",
"WEB-INF/messages_en.properties", etc.
XML properties files are also supported: .g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well.
basename
- the single basenamesetBasenames(java.lang.String...)
,
ResourceEditor
,
ResourceBundle
public void setBasenames(String... basenames)
ResourceBundleMessageSource
referring to a Spring resource location:
e.g. "WEB-INF/messages" for "WEB-INF/messages.properties",
"WEB-INF/messages_en.properties", etc.
XML properties files are also supported: .g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well.
The associated resource bundles will be checked sequentially when resolving a message code. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.
basenames
- an array of basenamessetBasename(java.lang.String)
,
ResourceBundle
public void setDefaultEncoding(String defaultEncoding)
Default is none, using the java.util.Properties
default encoding: ISO-8859-1.
Only applies to classic properties files, not to XML files.
defaultEncoding
- the default charsetsetFileEncodings(java.util.Properties)
,
PropertiesPersister.load(java.util.Properties, java.io.InputStream)
public void setFileEncodings(Properties fileEncodings)
Only applies to classic properties files, not to XML files.
fileEncodings
- Properties with filenames as keys and charset
names as values. Filenames have to match the basename syntax,
with optional locale-specific appendices: e.g. "WEB-INF/messages"
or "WEB-INF/messages_en".setBasenames(java.lang.String...)
,
PropertiesPersister.load(java.util.Properties, java.io.InputStream)
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale)
Falling back to the system Locale is the default behavior of
java.util.ResourceBundle
. However, this is often not desirable
in an application server environment, where the system Locale is not relevant
to the application at all: Set this flag to "false" in such a scenario.
public void setCacheSeconds(int cacheSeconds)
java.util.ResourceBundle
).
public void setConcurrentRefresh(boolean concurrentRefresh)
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".
setCacheSeconds(int)
public void setPropertiesPersister(PropertiesPersister propertiesPersister)
The default is a DefaultPropertiesPersister.
DefaultPropertiesPersister
public void setResourceLoader(ResourceLoader resourceLoader)
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.
setResourceLoader
in interface ResourceLoaderAware
resourceLoader
- ResourceLoader object to be used by this objectDefaultResourceLoader
,
ResourceLoaderAware
protected String resolveCodeWithoutArguments(String code, Locale locale)
resolveCodeWithoutArguments
in class AbstractMessageSource
code
- the code of the message to resolvelocale
- the Locale to resolve the code for
(subclasses are encouraged to support internationalization)null
if not foundAbstractMessageSource.resolveCode(java.lang.String, java.util.Locale)
,
MessageFormat
protected MessageFormat resolveCode(String code, Locale locale)
resolveCode
in class AbstractMessageSource
code
- the code of the message to resolvelocale
- the Locale to resolve the code for
(subclasses are encouraged to support internationalization)null
if not foundAbstractMessageSource.resolveCodeWithoutArguments(String, java.util.Locale)
protected ReloadableResourceBundleMessageSource.PropertiesHolder getMergedProperties(Locale locale)
Only used when caching resource bundle contents forever, i.e. with cacheSeconds < 0. Therefore, merged properties are always cached forever.
protected List<String> calculateAllFilenames(String basename, Locale locale)
basename
- the basename of the bundlelocale
- the localesetFallbackToSystemLocale(boolean)
,
calculateFilenamesForLocale(java.lang.String, java.util.Locale)
protected List<String> calculateFilenamesForLocale(String basename, Locale locale)
Follows the rules defined by Locale.toString()
.
basename
- the basename of the bundlelocale
- the localeprotected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties(String filename)
filename
- the bundle filename (basename + Locale)protected ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties(String filename, ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
null
if not cached before, or a timed-out cache entry
(potentially getting re-validated against the current last-modified timestamp).filename
- the bundle filename (basename + Locale)propHolder
- the current PropertiesHolder for the bundleprotected Properties loadProperties(Resource resource, String filename) throws IOException
resource
- the resource to load fromfilename
- the original bundle filename (basename + Locale)IOException
- if properties loading failedpublic void clearCache()
public void clearCacheIncludingAncestors()
clearCache()