org.springframework.context.support
Class ResourceBundleMessageSource

java.lang.Object
  extended by org.springframework.context.support.AbstractMessageSource
      extended by org.springframework.context.support.ResourceBundleMessageSource
All Implemented Interfaces:
HierarchicalMessageSource, MessageSource

public class ResourceBundleMessageSource
extends AbstractMessageSource

MessageSource that accesses the resource bundles with the specified basenames. This class relies on the underlying JDK's java.util.ResourceBundle implementation, in combination with the 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.

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.

Author:
Rod Johnson, Juergen Hoeller
See Also:
setBasenames(java.lang.String[]), ReloadableResourceBundleMessageSource, ResourceBundle, MessageFormat

Field Summary
 
Fields inherited from class org.springframework.context.support.AbstractMessageSource
logger
 
Constructor Summary
ResourceBundleMessageSource()
           
 
Method Summary
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  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: It is a fully-qualified classname.
 void setBasenames(String[] basenames)
          Set an array of basenames, each following ResourceBundle conventions.
 void setBundleClassLoader(ClassLoader classLoader)
          Set the ClassLoader to load resource bundles with.
 void setClassLoader(ClassLoader classLoader)
          Deprecated. in favor of setBundleClassLoader
 String toString()
          Show the configuration of this MessageSource.
 
Methods inherited from class org.springframework.context.support.AbstractMessageSource
createMessageFormat, formatMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isAlwaysUseMessageFormat, isUseCodeAsDefaultMessage, renderDefaultMessage, resolveArguments, setAlwaysUseMessageFormat, setParentMessageSource, setUseCodeAsDefaultMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ResourceBundleMessageSource

public ResourceBundleMessageSource()
Method Detail

setBasename

public void setBasename(String basename)
Set a single basename, following ResourceBundle conventions: It is a fully-qualified classname. 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 WAR. They can also be held in Jars on the class path. For example, a Jar in an application's manifest classpath could contain messages for the application.

Parameters:
basename - the single basename
See Also:
setBasenames(java.lang.String[]), ResourceBundle

setBasenames

public void setBasenames(String[] basenames)
Set an array of basenames, each following ResourceBundle conventions. 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.

Parameters:
basenames - an array of basenames
See Also:
setBasename(java.lang.String), ResourceBundle

setClassLoader

public void setClassLoader(ClassLoader classLoader)
Deprecated. in favor of setBundleClassLoader

Set the ClassLoader to load resource bundles with. Default is the thread context ClassLoader.

See Also:
setBundleClassLoader(java.lang.ClassLoader)

setBundleClassLoader

public void setBundleClassLoader(ClassLoader classLoader)
Set the ClassLoader to load resource bundles with. Default is the thread context ClassLoader.


getBundleClassLoader

protected ClassLoader getBundleClassLoader()
Return the ClassLoader to load resource bundles with. Default is the specified bundle ClassLoader, usually the thread context ClassLoader.


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 class AbstractMessageSource
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
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 class AbstractMessageSource
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
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 ResourceBundle
locale - 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 for
locale - the Locale to look for
Returns:
the corresponding ResourceBundle
Throws:
MissingResourceException - if no matching bundle could be found
See Also:
ResourceBundle.getBundle(String, java.util.Locale, ClassLoader), getBundleClassLoader()

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

toString

public String toString()
Show the configuration of this MessageSource.

Overrides:
toString in class Object


Copyright (c) 2002-2007 The Spring Framework Project.