org.springframework.context.support

Class 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'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".

    On the classpath, bundle resources will be read with the locally configured encoding: by default, ISO-8859-1; consider switching this to UTF-8, or to null for the platform default encoding. On the JDK 9+ module path where locally provided ResourceBundle.Control handles are not supported, this MessageSource always falls back to ResourceBundle.getBundle(java.lang.String) retrieval with the platform default encoding: UTF-8 with a ISO-8859-1 fallback on JDK 9+ (configurable through the "java.util.PropertyResourceBundle.encoding" system property). Note that loadBundle(Reader)/loadBundle(InputStream) won't be called in this case either, effectively ignoring overrides in subclasses. Consider implementing a JDK 9 java.util.spi.ResourceBundleProvider instead.

    Author:
    Rod Johnson, Juergen Hoeller
    See Also:
    AbstractResourceBasedMessageSource.setBasenames(java.lang.String...), ReloadableResourceBundleMessageSource, ResourceBundle, MessageFormat
    • Constructor Detail

      • ResourceBundleMessageSource

        public ResourceBundleMessageSource()
    • Method Detail

      • setBundleClassLoader

        public void setBundleClassLoader(java.lang.ClassLoader classLoader)
        Set the ClassLoader to load resource bundles with.

        Default is the containing BeanFactory's bean ClassLoader, or the default ClassLoader determined by ClassUtils.getDefaultClassLoader() if not running within a BeanFactory.

      • resolveCodeWithoutArguments

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

        @Nullable
        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.
        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

        @Nullable
        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.
        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 java.util.ResourceBundle doGetBundle(java.lang.String basename,
                                                       java.util.Locale locale)
                                                throws java.util.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:
        java.util.MissingResourceException - if no matching bundle could be found
        See Also:
        ResourceBundle.getBundle(String, Locale, ClassLoader), getBundleClassLoader()
      • loadBundle

        protected java.util.ResourceBundle loadBundle(java.io.Reader reader)
                                               throws java.io.IOException
        Load a property-based resource bundle from the given reader.

        This will be called in case of a "defaultEncoding", including ResourceBundleMessageSource's default ISO-8859-1 encoding. Note that this method can only be called with a ResourceBundle.Control: When running on the JDK 9+ module path where such control handles are not supported, any overrides in custom subclasses will effectively get ignored.

        The default implementation returns a PropertyResourceBundle.

        Parameters:
        reader - the reader for the target resource
        Returns:
        the fully loaded bundle
        Throws:
        java.io.IOException - in case of I/O failure
        Since:
        4.2
        See Also:
        loadBundle(InputStream), PropertyResourceBundle.PropertyResourceBundle(Reader)
      • loadBundle

        protected java.util.ResourceBundle loadBundle(java.io.InputStream inputStream)
                                               throws java.io.IOException
        Load a property-based resource bundle from the given input stream, picking up the default properties encoding on JDK 9+.

        This will only be called with "defaultEncoding" set to null, explicitly enforcing the platform default encoding (which is UTF-8 with a ISO-8859-1 fallback on JDK 9+ but configurable through the "java.util.PropertyResourceBundle.encoding" system property). Note that this method can only be called with a ResourceBundle.Control: When running on the JDK 9+ module path where such control handles are not supported, any overrides in custom subclasses will effectively get ignored.

        The default implementation returns a PropertyResourceBundle.

        Parameters:
        inputStream - the input stream for the target resource
        Returns:
        the fully loaded bundle
        Throws:
        java.io.IOException - in case of I/O failure
        Since:
        5.1
        See Also:
        loadBundle(Reader), PropertyResourceBundle.PropertyResourceBundle(InputStream)
      • getMessageFormat

        @Nullable
        protected java.text.MessageFormat getMessageFormat(java.util.ResourceBundle bundle,
                                                                     java.lang.String code,
                                                                     java.util.Locale locale)
                                                              throws java.util.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:
        java.util.MissingResourceException - if thrown by the ResourceBundle
      • getStringOrNull

        @Nullable
        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.

        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.

        Parameters:
        bundle - the ResourceBundle to perform the lookup in
        key - the key to look up
        Returns:
        the associated value, or null if none
        Since:
        4.2
        See Also:
        ResourceBundle.getString(String), ResourceBundle.containsKey(String)
      • toString

        public java.lang.String toString()
        Show the configuration of this MessageSource.
        Overrides:
        toString in class java.lang.Object