public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements org.springframework.beans.factory.BeanClassLoaderAware
MessageSource implementation that
accesses resource bundles using specified basenames. This class relies
on the underlying JDK's ResourceBundle implementation,
in combination with the JDK's standard message parsing provided by
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 ResourceBundle 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".
AbstractResourceBasedMessageSource.setBasenames(java.lang.String...),
ReloadableResourceBundleMessageSource,
ResourceBundle,
MessageFormatlogger| Constructor and Description |
|---|
ResourceBundleMessageSource() |
| Modifier and Type | Method and Description |
|---|---|
protected java.util.ResourceBundle |
doGetBundle(java.lang.String basename,
java.util.Locale locale)
Obtain the resource bundle for the given basename and Locale.
|
protected java.lang.ClassLoader |
getBundleClassLoader()
Return the ClassLoader to load resource bundles with.
|
protected java.text.MessageFormat |
getMessageFormat(java.util.ResourceBundle bundle,
java.lang.String code,
java.util.Locale locale)
Return a MessageFormat for the given bundle and code,
fetching already generated MessageFormats from the cache.
|
protected java.util.ResourceBundle |
getResourceBundle(java.lang.String basename,
java.util.Locale locale)
Return a ResourceBundle for the given basename and code,
fetching already generated MessageFormats from the cache.
|
protected java.lang.String |
getStringOrNull(java.util.ResourceBundle bundle,
java.lang.String key)
Efficiently retrieve the String value for the specified key,
or return
null if not found. |
protected java.util.ResourceBundle |
loadBundle(java.io.Reader reader)
Load a property-based resource bundle from the given reader.
|
protected java.text.MessageFormat |
resolveCode(java.lang.String code,
java.util.Locale locale)
Resolves the given message code as key in the registered resource bundles,
using a cached MessageFormat instance per message code.
|
protected java.lang.String |
resolveCodeWithoutArguments(java.lang.String code,
java.util.Locale locale)
Resolves the given message code as key in the registered resource bundles,
returning the value found in the bundle as-is (without MessageFormat parsing).
|
void |
setBeanClassLoader(java.lang.ClassLoader classLoader) |
void |
setBundleClassLoader(java.lang.ClassLoader classLoader)
Set the ClassLoader to load resource bundles with.
|
java.lang.String |
toString()
Show the configuration of this MessageSource.
|
addBasenames, getBasenameSet, getCacheMillis, getDefaultEncoding, isFallbackToSystemLocale, setBasename, setBasenames, setCacheMillis, setCacheSeconds, setDefaultEncoding, setFallbackToSystemLocalegetCommonMessages, getDefaultMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessagecreateMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormatpublic void setBundleClassLoader(java.lang.ClassLoader classLoader)
Default is the containing BeanFactory's
bean ClassLoader,
or the default ClassLoader determined by
ClassUtils.getDefaultClassLoader()
if not running within a BeanFactory.
protected java.lang.ClassLoader getBundleClassLoader()
Default is the containing BeanFactory's bean ClassLoader.
public void setBeanClassLoader(java.lang.ClassLoader classLoader)
setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAwareprotected java.lang.String resolveCodeWithoutArguments(java.lang.String code,
java.util.Locale locale)
resolveCodeWithoutArguments in class AbstractMessageSourcecode - 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),
MessageFormatprotected java.text.MessageFormat resolveCode(java.lang.String code,
java.util.Locale locale)
resolveCode in class AbstractMessageSourcecode - 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 java.util.ResourceBundle getResourceBundle(java.lang.String basename,
java.util.Locale locale)
basename - the basename of the ResourceBundlelocale - the Locale to find the ResourceBundle fornull if none
found for the given basename and Localeprotected java.util.ResourceBundle doGetBundle(java.lang.String basename,
java.util.Locale locale)
throws java.util.MissingResourceException
basename - the basename to look forlocale - the Locale to look forjava.util.MissingResourceException - if no matching bundle could be foundResourceBundle.getBundle(String, Locale, ClassLoader),
getBundleClassLoader()protected java.util.ResourceBundle loadBundle(java.io.Reader reader)
throws java.io.IOException
The default implementation returns a PropertyResourceBundle.
reader - the reader for the target resourcejava.io.IOException - in case of I/O failurePropertyResourceBundle.PropertyResourceBundle(Reader)protected java.text.MessageFormat getMessageFormat(java.util.ResourceBundle bundle,
java.lang.String code,
java.util.Locale locale)
throws java.util.MissingResourceException
bundle - the ResourceBundle to work oncode - the message code to retrievelocale - the Locale to use to build the MessageFormatnull if no message
defined for the given codejava.util.MissingResourceException - if thrown by the ResourceBundleprotected java.lang.String getStringOrNull(java.util.ResourceBundle bundle,
java.lang.String key)
null if not found.
As of 4.2, the default implementation checks containsKey
before it attempts to call getString (which would require
catching MissingResourceException for key not found).
Can be overridden in subclasses.
bundle - the ResourceBundle to perform the lookup inkey - the key to look upnull if noneResourceBundle.getString(String),
ResourceBundle.containsKey(String)public java.lang.String toString()
toString in class java.lang.Object