public class ResourceBundleMessageSource extends AbstractMessageSource implements 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.
Unfortunately, java.util.ResourceBundle
caches loaded bundles
forever: Reloading a bundle during VM execution is not possible.
As this MessageSource relies on ResourceBundle, it faces the same limitation.
Consider ReloadableResourceBundleMessageSource
for an alternative
that is capable of refreshing the underlying bundle files.
setBasenames(java.lang.String...)
,
ReloadableResourceBundleMessageSource
,
ResourceBundle
,
MessageFormat
logger
Constructor and Description |
---|
ResourceBundleMessageSource() |
Modifier and Type | Method and 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 return
null 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 |
setBasename(String basename)
Set a single basename, following
ResourceBundle conventions:
essentially, a fully-qualified classpath location. |
void |
setBasenames(String... basenames)
Set an array of basenames, each following
ResourceBundle
conventions: essentially, a fully-qualified classpath location. |
void |
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setBundleClassLoader(ClassLoader classLoader)
Set the ClassLoader to load resource bundles with.
|
void |
setCacheSeconds(int cacheSeconds)
Set the number of seconds to cache loaded resource bundle files.
|
void |
setDefaultEncoding(String defaultEncoding)
Set the default charset to use for parsing resource bundle files.
|
void |
setFallbackToSystemLocale(boolean fallbackToSystemLocale)
Set whether to fall back to the system Locale if no files for a specific
Locale have been found.
|
String |
toString()
Show the configuration of this MessageSource.
|
getCommonMessages, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage
createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat
public void setBasename(String basename)
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.
Messages will normally be held in the "/lib" or "/classes" directory of a web application's WAR structure. They can also be held in jar files on the class path.
Note that ResourceBundle names are effectively classpath locations: As a
consequence, the JDK's standard ResourceBundle treats dots as package separators.
This means that "test.theme" is effectively equivalent to "test/theme",
just like it is for programmatic java.util.ResourceBundle
usage.
public void setBasenames(String... basenames)
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.
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.
Note that ResourceBundle names are effectively classpath locations: As a
consequence, the JDK's standard ResourceBundle treats dots as package separators.
This means that "test.theme" is effectively equivalent to "test/theme",
just like it is for programmatic java.util.ResourceBundle
usage.
public void setDefaultEncoding(String defaultEncoding)
Default is the java.util.ResourceBundle
default encoding:
ISO-8859-1.
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)
ReloadableResourceBundleMessageSource
in combination
with resource bundle files in a non-classpath location.
public void setBundleClassLoader(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 ClassLoader getBundleClassLoader()
Default is the containing BeanFactory's bean ClassLoader.
public void setBeanClassLoader(ClassLoader classLoader)
BeanClassLoaderAware
class 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.
setBeanClassLoader
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
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 ResourceBundle getResourceBundle(String basename, Locale locale)
basename
- the basename of the ResourceBundlelocale
- the Locale to find the ResourceBundle fornull
if none
found for the given basename and Localeprotected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException
basename
- the basename to look forlocale
- the Locale to look forMissingResourceException
- if no matching bundle could be foundResourceBundle.getBundle(String, Locale, ClassLoader)
,
getBundleClassLoader()
protected ResourceBundle loadBundle(Reader reader) throws IOException
The default implementation returns a PropertyResourceBundle
.
reader
- the reader for the target resourceIOException
- in case of I/O failurePropertyResourceBundle.PropertyResourceBundle(Reader)
protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale) throws 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 codeMissingResourceException
- if thrown by the ResourceBundleprotected String getStringOrNull(ResourceBundle bundle, 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)