org.springframework.context.MessageSource implementation that
accesses resource bundles using specified basenames. This class relies
on the underlying JDK's java.util.ResourceBundle implementation,
in combination with the JDK's standard message parsing provided by
java.text.MessageFormat .
This MessageSource caches both the accessed ResourceBundle instances and
the generated MessageFormats for each message. It also implements rendering of
no-arg messages without MessageFormat, as supported by the AbstractMessageSource
base class. The caching provided by this MessageSource is significantly faster
than the built-in caching of the java.util.ResourceBundle class.
The basenames follow java. conventions: essentially,
a fully-qualified classpath location. If it doesn't contain a package qualifier
(such as org.mypackage), it will be resolved from the classpath root.
Note that the JDK's standard ResourceBundle treats dots as package separators:
This means that "test.theme" is effectively equivalent to "test/theme".
On the classpath, bundle resources will be read with the locally configured
encoding: by default, ISO-8859-1; consider switching
this to UTF-8, or to null for the platform default encoding. On the JDK 9+
module path where locally provided ResourceBundle.Control handles are not
supported, this MessageSource always falls back to ResourceBundle#getBundle
retrieval with the platform default encoding: UTF-8 with a ISO-8859-1 fallback on
JDK 9+ (configurable through the "java.util.PropertyResourceBundle.encoding" system
property). Note that loadBundle(Reader)/loadBundle(InputStream)
won't be called in this case either, effectively ignoring overrides in subclasses.
Consider implementing a JDK 9 java.util.spi.ResourceBundleProvider instead.
setBasenames, ReloadableResourceBundleMessageSource, java.util.ResourceBundle , java.text.MessageFormat
| Modifier and Type | Class and Description |
|---|---|
| private class | ResourceBundleMessageSource.
Custom implementation of |
| Modifier and Type | Field and Description |
|---|---|
| private ClassLoader | |
| private ClassLoader | |
| private final Map | cachedBundleMessageFormats
Cache to hold already generated MessageFormats. |
| private final Map | cachedResourceBundles
Cache to hold loaded ResourceBundles. |
| private volatile ResourceBundleMessageSource. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected ResourceBundle | Returns: the corresponding ResourceBundlethe basename to look for basename, Locale the Locale to look for locale)Obtain the resource bundle for the given basename and Locale. |
| protected ClassLoader | |
| protected MessageFormat | Returns: the resulting MessageFormat, ornull if no message
defined for the given codethe ResourceBundle to work on bundle, String the message code to retrieve code, Locale the Locale to use to build the MessageFormat locale)Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache. |
| protected ResourceBundle | Returns: the resulting ResourceBundle, ornull if none
found for the given basename and Localethe basename of the ResourceBundle basename, Locale the Locale to find the ResourceBundle for locale)Return a ResourceBundle for the given basename and Locale, fetching already generated ResourceBundle from the cache. |
| protected String | Returns: the associated value, ornull if nonethe ResourceBundle to perform the lookup in bundle, String the key to look up key)Efficiently retrieve the String value for the specified key,
or return |
| protected ResourceBundle | Returns: the fully loaded bundlethe reader for the target resource reader)Load a property-based resource bundle from the given reader. |
| protected ResourceBundle | Returns: the fully loaded bundlethe input stream for the target resource inputStream)Load a property-based resource bundle from the given input stream, picking up the default properties encoding on JDK 9+. |
| protected MessageFormat | resolveCode(String
the code of the message to resolve code, Locale the locale to resolve the code for
(subclasses are encouraged to support internationalization) locale)Implements abstract org. Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code. |
| protected String | resolveCodeWithoutArguments(String
the code of the message to resolve code, Locale the locale to resolve the code for
(subclasses are encouraged to support internationalization) locale)Overrides org. Resolves the given message code as key in the registered resource bundles, returning the value found in the bundle as-is (without MessageFormat parsing). |
| public void | setBeanClassLoader(ClassLoader
the owning class loader classLoader)Implements org. Callback that supplies the bean |
| public void | |
| public String |
| beanClassLoader | back to summary |
|---|---|
| private ClassLoader beanClassLoader
| |
| bundleClassLoader | back to summary |
|---|---|
| private ClassLoader bundleClassLoader
| |
| cachedBundleMessageFormats | back to summary |
|---|---|
| private final Map<ResourceBundle, Map<String, Map<Locale, MessageFormat>>> cachedBundleMessageFormats Cache to hold already generated MessageFormats. This Map is keyed with the ResourceBundle, which holds a Map that is keyed with the message code, which in turn holds a Map that is keyed with the Locale and holds the MessageFormat values. This allows for very efficient hash lookups without concatenated keys.
| |
| cachedResourceBundles | back to summary |
|---|---|
| private final Map<String, Map<Locale, ResourceBundle>> cachedResourceBundles Cache to hold loaded ResourceBundles. This Map is keyed with the bundle basename, which holds a Map that is keyed with the Locale and in turn holds the ResourceBundle instances. This allows for very efficient hash lookups, significantly faster than the ResourceBundle class's own cache. | |
| control | back to summary |
|---|---|
| private volatile ResourceBundleMessageSource. | |
| ResourceBundleMessageSource | back to summary |
|---|---|
| public ResourceBundleMessageSource() | |
| doGetBundle | back to summary |
|---|---|
| protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException Obtain the resource bundle for the given basename and Locale.
| |
| getBundleClassLoader | back to summary |
|---|---|
| protected ClassLoader getBundleClassLoader() Return the ClassLoader to load resource bundles with. Default is the containing BeanFactory's bean ClassLoader.
| |
| getMessageFormat | back to summary |
|---|---|
| protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale) throws MissingResourceException Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.
| |
| getResourceBundle | back to summary |
|---|---|
| protected ResourceBundle getResourceBundle(String basename, Locale locale) Return a ResourceBundle for the given basename and Locale, fetching already generated ResourceBundle from the cache.
| |
| getStringOrNull | back to summary |
|---|---|
| protected String getStringOrNull(ResourceBundle bundle, String key) Efficiently retrieve the String value for the specified key,
or return As of 4.2, the default implementation checks Can be overridden in subclasses.
| |
| loadBundle | back to summary |
|---|---|
| protected ResourceBundle loadBundle(Reader reader) throws IOException Load a property-based resource bundle from the given reader. This will be called in case of a The default implementation returns a
| |
| loadBundle | back to summary |
|---|---|
| protected ResourceBundle loadBundle(InputStream inputStream) throws IOException Load a property-based resource bundle from the given input stream, picking up the default properties encoding on JDK 9+. This will only be called with The default implementation returns a
| |
| resolveCode | back to summary |
|---|---|
| protected MessageFormat resolveCode(String code, Locale locale) Implements abstract org. Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.
| |
| resolveCodeWithoutArguments | back to summary |
|---|---|
| protected String resolveCodeWithoutArguments(String code, Locale locale) Overrides org. Resolves the given message code as key in the registered resource bundles, returning the value found in the bundle as-is (without MessageFormat parsing).
| |
| setBeanClassLoader | back to summary |
|---|---|
| public void setBeanClassLoader(ClassLoader classLoader) Implements org. Doc from org. Callback that supplies the bean Invoked after the population of normal bean properties but
before an initialization callback such as
| |
| setBundleClassLoader | back to summary |
|---|---|
| public void setBundleClassLoader(ClassLoader classLoader) Set the ClassLoader to load resource bundles with. Default is the containing BeanFactory's
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Show the configuration of this MessageSource.
| |
ResourceBundle.Control, adding support
for custom file encodings, deactivating the fallback to the system locale
and activating ResourceBundle's native cache, if desired.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Locale | getFallbackLocale(String
the base name of the resource bundle, a fully
qualified class name for which
baseName, Locale ResourceBundle.getBundle has been
unable to find any resource bundles (except for the
base bundle)the locale)Locale for which
ResourceBundle.getBundle has been
unable to find any resource bundles (except for the
base bundle)Overrides java. Returns a |
| public long | getTimeToLive(String
the base name of the resource bundle for which the
expiration value is specified. baseName, Locale the locale of the resource bundle for which the
expiration value is specified. locale)Overrides java. Returns the time-to-live (TTL) value for resource bundles that
are loaded under this
|
| public boolean | needsReload(String
the base bundle name of the resource bundle, a
fully qualified class name baseName, Locale the locale for which the resource bundle
should be instantiated locale, String the resource bundle format to be loaded format, ClassLoader the loader, ResourceBundle ClassLoader to use to load the bundlethe resource bundle instance that has been expired
in the cache bundle, long the time when loadTime)bundle was loaded and put
in the cacheOverrides java. Determines if the expired |
| public ResourceBundle | newBundle(String
the base bundle name of the resource bundle, a fully
qualified class name baseName, Locale the locale for which the resource bundle should be
instantiated locale, String the resource bundle format to be loaded format, ClassLoader the loader, boolean ClassLoader to use to load the bundlethe flag to indicate bundle reloading; reload)true
if reloading an expired resource bundle,
false otherwiseOverrides java. Instantiates a resource bundle for the given bundle name of the given format and locale, using the given class loader if necessary. |
| MessageSourceControl | back to summary |
|---|---|
| private MessageSourceControl() | |
| getFallbackLocale | back to summary |
|---|---|
| public Locale getFallbackLocale(String baseName, Locale locale) Overrides java. Doc from java. Returns a The method returns The default implementation returns the default
| |
| getTimeToLive | back to summary |
|---|---|
| public long getTimeToLive(String baseName, Locale locale) Overrides java. Doc from java. Returns the time-to-live (TTL) value for resource bundles that
are loaded under this
The expiration affects only the bundle loading process by the
All cached resource bundles are subject to removal from the cache due to memory constraints of the runtime environment. Returning a large positive value doesn't mean to lock loaded resource bundles in the cache. The default implementation returns
| |
| needsReload | back to summary |
|---|---|
| public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) Overrides java. Doc from java. Determines if the expired
The calling The default implementation compares
| |
| newBundle | back to summary |
|---|---|
| public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException Overrides java. Doc from java. Instantiates a resource bundle for the given bundle name of the
given format and locale, using the given class loader if
necessary. This method returns If the
| |