org.springframework.context.support

Class ReloadableResourceBundleMessageSource

    • Constructor Detail

      • ReloadableResourceBundleMessageSource

        public ReloadableResourceBundleMessageSource()
    • Method Detail

      • setConcurrentRefresh

        public void setConcurrentRefresh(boolean concurrentRefresh)
        Specify whether to allow for concurrent refresh behavior, i.e. one thread locked in a refresh attempt for a specific cached properties file whereas other threads keep returning the old properties for the time being, until the refresh attempt has completed.

        Default is "true": this behavior is new as of Spring Framework 4.1, minimizing contention between threads. If you prefer the old behavior, i.e. to fully block on refresh, switch this flag to "false".

        Since:
        4.1
        See Also:
        AbstractResourceBasedMessageSource.setCacheSeconds(int)
      • setResourceLoader

        public void setResourceLoader(@Nullable
                                      ResourceLoader resourceLoader)
        Set the ResourceLoader to use for loading bundle properties files.

        The default is a DefaultResourceLoader. Will get overridden by the ApplicationContext if running in a context, as it implements the ResourceLoaderAware interface. Can be manually overridden when running outside of an ApplicationContext.

        Specified by:
        setResourceLoader in interface ResourceLoaderAware
        Parameters:
        resourceLoader - the ResourceLoader object to be used by this object
        See Also:
        DefaultResourceLoader, ResourceLoaderAware
      • resolveCodeWithoutArguments

        protected java.lang.String resolveCodeWithoutArguments(java.lang.String code,
                                                               java.util.Locale locale)
        Resolves the given message code as key in the retrieved bundle files, 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 retrieved bundle files, 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)
      • getMergedProperties

        protected ReloadableResourceBundleMessageSource.PropertiesHolder getMergedProperties(java.util.Locale locale)
        Get a PropertiesHolder that contains the actually visible properties for a Locale, after merging all specified resource bundles. Either fetches the holder from the cache or freshly loads it.

        Only used when caching resource bundle contents forever, i.e. with cacheSeconds < 0. Therefore, merged properties are always cached forever.

      • calculateFilenamesForLocale

        protected java.util.List<java.lang.String> calculateFilenamesForLocale(java.lang.String basename,
                                                                               java.util.Locale locale)
        Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code. E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO", "messages_de_AT", "messages_de".

        Follows the rules defined by Locale.toString().

        Parameters:
        basename - the basename of the bundle
        locale - the locale
        Returns:
        the List of filenames to check
      • getProperties

        protected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties(java.lang.String filename)
        Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.
        Parameters:
        filename - the bundle filename (basename + Locale)
        Returns:
        the current PropertiesHolder for the bundle
      • loadProperties

        protected java.util.Properties loadProperties(Resource resource,
                                                      java.lang.String filename)
                                               throws java.io.IOException
        Load the properties from the given resource.
        Parameters:
        resource - the resource to load from
        filename - the original bundle filename (basename + Locale)
        Returns:
        the populated Properties instance
        Throws:
        java.io.IOException - if properties loading failed
      • newProperties

        protected java.util.Properties newProperties()
        Template method for creating a plain new Properties instance. The default implementation simply calls Properties.Properties().

        Allows for returning a custom Properties extension in subclasses. Overriding methods should just instantiate a custom Properties subclass, with no further initialization or population to be performed at that point.

        Returns:
        a plain Properties instance
        Since:
        4.2
      • clearCache

        public void clearCache()
        Clear the resource bundle cache. Subsequent resolve calls will lead to reloading of the properties files.
      • clearCacheIncludingAncestors

        public void clearCacheIncludingAncestors()
        Clear the resource bundle caches of this MessageSource and all its ancestors.
        See Also:
        clearCache()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object