public class

ResourceBundleMessageSource

extends AbstractMessageSource
implements BeanClassLoaderAware
java.lang.Object
   ↳ org.springframework.context.support.MessageSourceSupport
     ↳ org.springframework.context.support.AbstractMessageSource
       ↳ org.springframework.context.support.ResourceBundleMessageSource

Class Overview

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.

Summary

[Expand]
Inherited Fields
From class org.springframework.context.support.MessageSourceSupport
Public Constructors
ResourceBundleMessageSource()
Public Methods
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.
String toString()
Show the configuration of this MessageSource.
Protected Methods
ResourceBundle doGetBundle(String basename, Locale locale)
Obtain the resource bundle for the given basename and Locale.
ClassLoader getBundleClassLoader()
Return the ClassLoader to load resource bundles with.
MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale)
Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.
ResourceBundle getResourceBundle(String basename, Locale locale)
Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.
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.
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).
[Expand]
Inherited Methods
From class org.springframework.context.support.AbstractMessageSource
From class org.springframework.context.support.MessageSourceSupport
From class java.lang.Object
From interface org.springframework.beans.factory.BeanClassLoaderAware
From interface org.springframework.context.HierarchicalMessageSource
From interface org.springframework.context.MessageSource

Public Constructors

public ResourceBundleMessageSource ()

Public Methods

public void setBasename (String basename)

Set a single basename, following 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)

Set an array of basenames, each following 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 setBeanClassLoader (ClassLoader classLoader)

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's afterPropertiesSet() method or a custom init-method.

Parameters
classLoader the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via getDefaultClassLoader()

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 by getDefaultClassLoader() if not running within a BeanFactory.

public String toString ()

Show the configuration of this MessageSource.

Protected Methods

protected ResourceBundle doGetBundle (String basename, Locale locale)

Obtain the resource bundle for the given basename and Locale.

Parameters
basename the basename to look for
locale the Locale to look for
Returns
  • the corresponding ResourceBundle
Throws
MissingResourceException if no matching bundle could be found

protected ClassLoader getBundleClassLoader ()

Return the ClassLoader to load resource bundles with.

Default is the containing BeanFactory's bean ClassLoader.

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.

Parameters
bundle the ResourceBundle to work on
code the message code to retrieve
locale 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

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 ResourceBundle
locale the Locale to find the ResourceBundle for
Returns
  • the resulting ResourceBundle, or null if none found for the given basename and Locale

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.

Parameters
code the code of the message to resolve
locale the Locale to resolve the code for (subclasses are encouraged to support internationalization)
Returns
  • the MessageFormat for the message, or null if not found

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).

Parameters
code the code of the message to resolve
locale the Locale to resolve the code for (subclasses are encouraged to support internationalization)
Returns
  • the message String, or null if not found