Class ResourceBundleMessageSource
- java.lang.Object
- org.springframework.context.support.MessageSourceSupport
- org.springframework.context.support.AbstractMessageSource
- org.springframework.context.support.AbstractResourceBasedMessageSource
- org.springframework.context.support.ResourceBundleMessageSource
- All Implemented Interfaces:
Aware
,BeanClassLoaderAware
,HierarchicalMessageSource
,MessageSource
public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements BeanClassLoaderAware
MessageSource
implementation that accesses resource bundles using specified basenames. This class relies on the underlying JDK'sResourceBundle
implementation, in combination with the JDK's standard message parsing provided byMessageFormat
.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 asorg.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".- Author:
- Rod Johnson, Juergen Hoeller
- See Also:
AbstractResourceBasedMessageSource.setBasenames(java.lang.String...)
,ReloadableResourceBundleMessageSource
,ResourceBundle
,MessageFormat
Field Summary
Fields inherited from class org.springframework.context.support.MessageSourceSupport
logger
Constructor Summary
Constructors Constructor Description ResourceBundleMessageSource()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ResourceBundle
doGetBundle(String basename, Locale locale)
Obtain the resource bundle for the given basename and Locale.protected ClassLoader
getBundleClassLoader()
Return the ClassLoader to load resource bundles with.protected MessageFormat
getMessageFormat(ResourceBundle bundle, String code, Locale locale)
Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.protected ResourceBundle
getResourceBundle(String basename, Locale locale)
Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.protected String
getStringOrNull(ResourceBundle bundle, String key)
Efficiently retrieve the String value for the specified key, or returnnull
if not found.protected ResourceBundle
loadBundle(Reader reader)
Load a property-based resource bundle from the given reader.protected MessageFormat
resolveCode(String code, Locale locale)
Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.protected String
resolveCodeWithoutArguments(String code, 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(ClassLoader classLoader)
Callback that supplies the beanclass loader
to a bean instance.void
setBundleClassLoader(ClassLoader classLoader)
Set the ClassLoader to load resource bundles with.String
toString()
Show the configuration of this MessageSource.Methods inherited from class org.springframework.context.support.AbstractResourceBasedMessageSource
addBasenames, getBasenameSet, getCacheMillis, getDefaultEncoding, isFallbackToSystemLocale, setBasename, setBasenames, setCacheMillis, setCacheSeconds, setDefaultEncoding, setFallbackToSystemLocale
Methods inherited from class org.springframework.context.support.AbstractMessageSource
getCommonMessages, getDefaultMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage
Methods inherited from class org.springframework.context.support.MessageSourceSupport
createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat
Constructor Detail
ResourceBundleMessageSource
public ResourceBundleMessageSource()
Method Detail
setBundleClassLoader
public void setBundleClassLoader(ClassLoader classLoader)
Set the ClassLoader to load resource bundles with.Default is the containing BeanFactory's
bean ClassLoader
, or the default ClassLoader determined byClassUtils.getDefaultClassLoader()
if not running within a BeanFactory.
getBundleClassLoader
protected ClassLoader getBundleClassLoader()
Return the ClassLoader to load resource bundles with.Default is the containing BeanFactory's bean ClassLoader.
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
Description copied from interface:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- Specified by:
setBeanClassLoader
in interfaceBeanClassLoaderAware
- Parameters:
classLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
resolveCodeWithoutArguments
protected String resolveCodeWithoutArguments(String code, 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).- Overrides:
resolveCodeWithoutArguments
in classAbstractMessageSource
- Parameters:
code
- the code of the message to resolvelocale
- the locale to resolve the code for (subclasses are encouraged to support internationalization)- Returns:
- the message String, or
null
if not found - See Also:
AbstractMessageSource.resolveCode(java.lang.String, java.util.Locale)
,MessageFormat
resolveCode
protected MessageFormat resolveCode(String code, Locale locale)
Resolves the given message code as key in the registered resource bundles, using a cached MessageFormat instance per message code.- Specified by:
resolveCode
in classAbstractMessageSource
- Parameters:
code
- the code of the message to resolvelocale
- the locale to resolve the code for (subclasses are encouraged to support internationalization)- Returns:
- the MessageFormat for the message, or
null
if not found - See Also:
AbstractMessageSource.resolveCodeWithoutArguments(String, java.util.Locale)
getResourceBundle
protected ResourceBundle getResourceBundle(String basename, Locale locale)
Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.- Parameters:
basename
- the basename of the ResourceBundlelocale
- the Locale to find the ResourceBundle for- Returns:
- the resulting ResourceBundle, or
null
if none found for the given basename and Locale
doGetBundle
protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException
Obtain the resource bundle for the given basename and Locale.- Parameters:
basename
- the basename to look forlocale
- the Locale to look for- Returns:
- the corresponding ResourceBundle
- Throws:
MissingResourceException
- if no matching bundle could be found- See Also:
ResourceBundle.getBundle(String, Locale, ClassLoader)
,getBundleClassLoader()
loadBundle
protected ResourceBundle loadBundle(Reader reader) throws IOException
Load a property-based resource bundle from the given reader.The default implementation returns a
PropertyResourceBundle
.- Parameters:
reader
- the reader for the target resource- Returns:
- the fully loaded bundle
- Throws:
IOException
- in case of I/O failure- Since:
- 4.2
- See Also:
PropertyResourceBundle(Reader)
getMessageFormat
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.- Parameters:
bundle
- the ResourceBundle to work oncode
- the message code to retrievelocale
- the Locale to use to build the MessageFormat- Returns:
- the resulting MessageFormat, or
null
if no message defined for the given code - Throws:
MissingResourceException
- if thrown by the ResourceBundle
getStringOrNull
protected String getStringOrNull(ResourceBundle bundle, String key)
Efficiently retrieve the String value for the specified key, or returnnull
if not found.As of 4.2, the default implementation checks
containsKey
before it attempts to callgetString
(which would require catchingMissingResourceException
for key not found).Can be overridden in subclasses.
- Parameters:
bundle
- the ResourceBundle to perform the lookup inkey
- the key to look up- Returns:
- the associated value, or
null
if none - Since:
- 4.2
- See Also:
ResourceBundle.getString(String)
,ResourceBundle.containsKey(String)