java.util.Properties instances as its internal
data structure for messages, loading them via a PropertiesPersister strategy:
The default strategy can load properties files with a specific encoding.
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 do 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 are not reflected in the application.
Note that the base names set as the "basename" and "basenames" properties are treated in a slightly different fashion than the "basename" 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 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 don't make a significant difference.
This MessageSource can easily be used outside an ApplicationContext: It uses a DefaultResourceLoader as default, getting overridden with the ApplicationContext 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!
| Fields inherited from org.springframework.context.supportAbstractMessageSource |
|---|
| Constructor Summary |
|---|
|
No description provided. |
| Method Summary | |
|---|---|
| protected List |
Calculate all filenames for the given bundle basename and Locale. |
| protected List |
Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code. |
| void |
Clear the resource bundle cache. |
| void |
Clear the resource bundle caches of this MessageSource and all its ancestors. |
| protected ReloadableResourceBundleMessageSource.PropertiesHolder |
Get a PropertiesHolder that contains the actually visible properties for a Locale, after merging all specified resource bundles. |
| protected ReloadableResourceBundleMessageSource.PropertiesHolder |
Get a PropertiesHolder for the given filename, either from the cache or freshly loaded. |
| protected Properties |
Load the properties from the given resource. |
| protected ReloadableResourceBundleMessageSource.PropertiesHolder |
Refresh the PropertiesHolder for the given bundle filename. |
| protected MessageFormat |
Resolves the given message code as key in the retrieved bundle files, using a cached MessageFormat instance per message code. |
| protected String |
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 |
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 |
Set an array of basenames, each following the above-mentioned special convention. |
| void |
Set the number of seconds to cache loaded properties files. |
| void |
Set the default charset to use for parsing properties files. |
| void |
Set whether to fall back to the system Locale if no files for a specific Locale have been found. |
| void |
Set per-file charsets to use for parsing properties files. |
| void |
Set the PropertiesPersister to use for parsing properties files. |
| void |
Set the ResourceLoader to use for loading bundle properties files. |
| String |
No description provided. |
| Methods inherited from org.springframework.contextHierarchicalMessageSource |
|---|
| Methods inherited from java.langObject |
|---|
public
void
clearCache
(
)
public
void
clearCacheIncludingAncestors
(
)
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
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). As of Spring 1.2.2, XML properties files are also supported: e.g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well. Note that this will only work on JDK 1.5+.
Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.
public
void
setCacheSeconds
(
int
cacheSeconds
)
java.util.ResourceBundle).
Default is none, using the java.util.Properties
default encoding.
Only applies to classic properties files, not to XML files.
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.
Only applies to classic properties files, not to XML files.
The default is a DefaultPropertiesPersister.
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.