org.springframework.context.MessageSource implementation
that accesses resource bundles using specified basenames, participating in the
Spring org.springframework.context.ApplicationContext 's resource loading.
In contrast to the JDK-based ResourceBundleMessageSource, this class uses
java. instances as its custom data structure for messages,
loading them via a org. strategy
from Spring Resource handles. This strategy is not only capable of
reloading files based on timestamp changes, but also of loading properties files
with a specific character encoding. It will detect XML property files as well.
Note that the basenames set as "basenames" property
are treated in a slightly different fashion than the "basenames" property of
ResourceBundleMessageSource. It follows the basic ResourceBundle rule of not
specifying file extension or language codes, but can refer to any Spring resource
location (instead of being restricted to classpath resources). With a "classpath:"
prefix, resources can still be loaded from the classpath, but "cacheSeconds" values
other than "-1" (caching forever) might not work reliably in this case.
For a typical web application, message files could be placed in WEB-INF:
e.g. a "WEB-INF/messages" basename would find a "WEB-INF/messages.properties",
"WEB-INF/messages_en.properties" etc arrangement as well as "WEB-INF/messages.xml",
"WEB-INF/messages_en.xml" etc. Note that message definitions in a previous
resource bundle will override ones in a later bundle, due to sequential lookup.
This MessageSource can easily be used outside an
org.: it will use a
org. as default,
simply getting overridden with the ApplicationContext's resource loader
if running in a context. It does not have any other specific dependencies.
Thanks to Thomas Achleitner for providing the initial implementation of this message source!
setCacheSeconds, setBasenames, setDefaultEncoding, setFileEncodings, setPropertiesPersister, setResourceLoader, org.springframework.core.io.DefaultResourceLoader , ResourceBundleMessageSource, java.util.ResourceBundle
| Modifier and Type | Class and Description |
|---|---|
| protected class | ReloadableResourceBundleMessageSource.PropertiesHolder
PropertiesHolder for caching. |
| Modifier and Type | Field and Description |
|---|---|
| private final ConcurrentMap | |
| private final ConcurrentMap | |
| private final ConcurrentMap | |
| private boolean | |
| private Properties | |
| private List | |
| private PropertiesPersister | |
| private ResourceLoader | |
| private static final String |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected List | Returns: the List of filenames to checkthe basename of the bundle basename, Locale the locale locale)Calculate all filenames for the given bundle basename and Locale. |
| protected List | Returns: the List of filenames to checkthe basename of the bundle basename, Locale the locale locale)Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code. |
| public void | |
| public void | clearCacheIncludingAncestors()
Clear the resource bundle caches of this MessageSource and all its ancestors. |
| protected List | Returns: the list of properties holdersthe locale locale)Determine the properties to merge based on the specified basenames. |
| protected ReloadableResourceBundleMessageSource. | getMergedProperties(Locale locale)
Get a PropertiesHolder that contains the actually visible properties for a Locale, after merging all specified resource bundles. |
| protected ReloadableResourceBundleMessageSource. | Returns: the current PropertiesHolder for the bundlethe bundle filename (basename + Locale) filename)Get a PropertiesHolder for the given filename, either from the cache or freshly loaded. |
| protected Properties | Returns: the populated Properties instancethe resource to load from resource, String the original bundle filename (basename + Locale) filename)Load the properties from the given resource. |
| protected ReloadableResourceBundleMessageSource. | Returns: a single merged properties holderthe list of properties holders holdersMerge the given properties holders into a single holder. |
| protected Properties | Returns: a plain Properties instanceTemplate method for creating a plain new |
| protected ReloadableResourceBundleMessageSource. | refreshProperties(String
the bundle filename (basename + Locale) filename, ReloadableResourceBundleMessageSource.the current PropertiesHolder for the bundle propHolderRefresh the PropertiesHolder for the given bundle filename. |
| protected MessageFormat | resolveCode(String
the code of the message to resolve code, Locale the locale to resolve the code for
(subclasses are encouraged to support internationalization) locale)Implements abstract org. |
| protected String | resolveCodeWithoutArguments(String
the code of the message to resolve code, Locale the locale to resolve the code for
(subclasses are encouraged to support internationalization) locale)Overrides org. |
| protected Resource | Returns: theResource to use, or null if none foundthe bundle filename (basename + Locale) filename)Resolve the specified bundle |
| 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. |
| public void | setFileEncodings(Properties
a Properties with filenames as keys and charset
names as values. Filenames have to match the basename syntax,
with optional locale-specific components: e.g. "WEB-INF/messages"
or "WEB-INF/messages_en". fileEncodings)Set per-file charsets to use for parsing properties files. |
| public void | setFileExtensions(List<String>
the file extensions (starts with a dot) fileExtensions)Set the list of supported file extensions. |
| public void | setPropertiesPersister(PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files. |
| public void | setResourceLoader(ResourceLoader
the ResourceLoader object to be used by this object resourceLoader)Implements org. |
| public String |
| cachedFilenames | back to summary |
|---|---|
| private final ConcurrentMap<String, Map<Locale, List<String>>> cachedFilenames | |
| cachedMergedProperties | back to summary |
|---|---|
| private final ConcurrentMap<Locale, ReloadableResourceBundleMessageSource. | |
| cachedProperties | back to summary |
|---|---|
| private final ConcurrentMap<String, ReloadableResourceBundleMessageSource. | |
| concurrentRefresh | back to summary |
|---|---|
| private boolean concurrentRefresh | |
| fileEncodings | back to summary |
|---|---|
| private Properties fileEncodings
| |
| fileExtensions | back to summary |
|---|---|
| private List<String> fileExtensions | |
| propertiesPersister | back to summary |
|---|---|
| private PropertiesPersister propertiesPersister | |
| resourceLoader | back to summary |
|---|---|
| private ResourceLoader resourceLoader | |
| XML_EXTENSION | back to summary |
|---|---|
| private static final String XML_EXTENSION | |
| ReloadableResourceBundleMessageSource | back to summary |
|---|---|
| public ReloadableResourceBundleMessageSource() | |
| calculateAllFilenames | back to summary |
|---|---|
| protected List Calculate all filenames for the given bundle basename and Locale. Will calculate filenames for the given Locale, the system Locale (if applicable), and the default file.
| |
| calculateFilenamesForLocale | back to summary |
|---|---|
| protected List Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code. For example, basename "messages", Locale "de_AT_oo" → "messages_de_AT_OO", "messages_de_AT", "messages_de". Follows the rules defined by | |
| clearCache | back to summary |
|---|---|
| public void clearCache() Clear the resource bundle cache. Subsequent resolve calls will lead to reloading of the properties files. | |
| clearCacheIncludingAncestors | back to summary |
|---|---|
| public void clearCacheIncludingAncestors() Clear the resource bundle caches of this MessageSource and all its ancestors.
| |
| collectPropertiesToMerge | back to summary |
|---|---|
| protected List Determine the properties to merge based on the specified basenames.
| |
| getMergedProperties | back to summary |
|---|---|
| protected ReloadableResourceBundleMessageSource. 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.
| |
| getProperties | back to summary |
|---|---|
| protected ReloadableResourceBundleMessageSource. Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.
| |
| loadProperties | back to summary |
|---|---|
| protected Properties loadProperties(Resource resource, String filename) throws IOException Load the properties from the given resource.
| |
| mergeProperties | back to summary |
|---|---|
| protected ReloadableResourceBundleMessageSource. Merge the given properties holders into a single holder.
| |
| newProperties | back to summary |
|---|---|
| protected Properties newProperties() Template method for creating a plain new Allows for returning a custom
| |
| refreshProperties | back to summary |
|---|---|
| protected ReloadableResourceBundleMessageSource. Refresh the PropertiesHolder for the given bundle filename. The holder can be
| |
| resolveCode | back to summary |
|---|---|
| protected MessageFormat resolveCode(String code, Locale locale) Implements abstract org. Resolves the given message code as key in the retrieved bundle files, using a cached MessageFormat instance per message code.
| |
| resolveCodeWithoutArguments | back to summary |
|---|---|
| protected String resolveCodeWithoutArguments(String code, Locale locale) Overrides org. Resolves the given message code as key in the retrieved bundle files, returning the value found in the bundle as-is (without MessageFormat parsing).
| |
| resolveResource | back to summary |
|---|---|
| protected Resource resolveResource(String filename) Resolve the specified bundle If no suitable concrete This can be leveraged to check the last modification timestamp or to load properties from alternative sources — for example, from an XML BLOB in a database, or from properties serialized using a custom format such as JSON. The default implementation delegates to the configured
When overriding this method, As an alternative to overriding this method, you can configure a
| |
| setConcurrentRefresh | back to summary |
|---|---|
| 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".
| |
| setFileEncodings | back to summary |
|---|---|
| public void setFileEncodings(Properties fileEncodings) Set per-file charsets to use for parsing properties files. Only applies to classic properties files, not to XML files.
| |
| setFileExtensions | back to summary |
|---|---|
| public void setFileExtensions(List<String> fileExtensions) Set the list of supported file extensions. The default is a list containing | |
| setPropertiesPersister | back to summary |
|---|---|
| public void setPropertiesPersister(PropertiesPersister propertiesPersister) Set the PropertiesPersister to use for parsing properties files. The default is
| |
| setResourceLoader | back to summary |
|---|---|
| public void setResourceLoader(ResourceLoader resourceLoader) Implements org. 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 an ApplicationContext.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private final ConcurrentMap | cachedMessageFormats
Cache to hold already generated MessageFormats per message code. |
| private final long | |
| private final Properties | |
| private final Lock | |
| private volatile long |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public long | |
| public MessageFormat | |
| public Properties | |
| public String | |
| public long | |
| public void |
| cachedMessageFormats | back to summary |
|---|---|
| private final ConcurrentMap<String, Map<Locale, MessageFormat>> cachedMessageFormats Cache to hold already generated MessageFormats per message code. | |
| fileTimestamp | back to summary |
|---|---|
| private final long fileTimestamp | |
| properties | back to summary |
|---|---|
| private final Properties properties
| |
| refreshLock | back to summary |
|---|---|
| private final Lock refreshLock | |
| refreshTimestamp | back to summary |
|---|---|
| private volatile long refreshTimestamp | |
| PropertiesHolder | back to summary |
|---|---|
| public PropertiesHolder() | |
| PropertiesHolder | back to summary |
|---|---|
| public PropertiesHolder(Properties properties, long fileTimestamp) | |
| getFileTimestamp | back to summary |
|---|---|
| public long getFileTimestamp() | |
| getMessageFormat | back to summary |
|---|---|
public MessageFormat getMessageFormat(String code, Locale locale)
| |
| getProperties | back to summary |
|---|---|
public Properties getProperties()
| |
| getProperty | back to summary |
|---|---|
public String getProperty(String code)
| |
| getRefreshTimestamp | back to summary |
|---|---|
| public long getRefreshTimestamp() | |
| setRefreshTimestamp | back to summary |
|---|---|
| public void setRefreshTimestamp(long refreshTimestamp) | |