类 ReloadableResourceBundleMessageSource
- java.lang.Object
- org.springframework.context.support.MessageSourceSupport
- org.springframework.context.support.AbstractMessageSource
- org.springframework.context.support.AbstractResourceBasedMessageSource
- org.springframework.context.support.ReloadableResourceBundleMessageSource
public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements ResourceLoaderAware
Spring-specificMessageSource
implementation that accesses resource bundles using specified basenames, participating in the SpringApplicationContext
's resource loading.In contrast to the JDK-based
ResourceBundleMessageSource
, this class usesProperties
instances as its custom data structure for messages, loading them via aPropertiesPersister
strategy from SpringResource
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 ofResourceBundleMessageSource
. 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 of an
ApplicationContext
: it will use aDefaultResourceLoader
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!
- 作者:
- Juergen Hoeller
- 另请参阅:
AbstractResourceBasedMessageSource.setCacheSeconds(int)
,AbstractResourceBasedMessageSource.setBasenames(java.lang.String...)
,AbstractResourceBasedMessageSource.setDefaultEncoding(java.lang.String)
,setFileEncodings(java.util.Properties)
,setPropertiesPersister(org.springframework.util.PropertiesPersister)
,setResourceLoader(org.springframework.core.io.ResourceLoader)
,DefaultPropertiesPersister
,DefaultResourceLoader
,ResourceBundleMessageSource
,ResourceBundle
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected class
ReloadableResourceBundleMessageSource.PropertiesHolder
PropertiesHolder for caching.
字段概要
从类继承的字段 org.springframework.context.support.MessageSourceSupport
logger
构造器概要
构造器 构造器 说明 ReloadableResourceBundleMessageSource()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected List<String>
calculateAllFilenames(String basename, Locale locale)
Calculate all filenames for the given bundle basename and Locale.protected List<String>
calculateFilenamesForLocale(String basename, Locale locale)
Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code.void
clearCache()
Clear the resource bundle cache.void
clearCacheIncludingAncestors()
Clear the resource bundle caches of this MessageSource and all its ancestors.protected ReloadableResourceBundleMessageSource.PropertiesHolder
getMergedProperties(Locale locale)
Get a PropertiesHolder that contains the actually visible properties for a Locale, after merging all specified resource bundles.protected ReloadableResourceBundleMessageSource.PropertiesHolder
getProperties(String filename)
Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.protected Properties
loadProperties(Resource resource, String filename)
Load the properties from the given resource.protected Properties
newProperties()
Template method for creating a plain newProperties
instance.protected ReloadableResourceBundleMessageSource.PropertiesHolder
refreshProperties(String filename, ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
Refresh the PropertiesHolder for the given bundle filename.protected MessageFormat
resolveCode(String code, Locale locale)
Resolves the given message code as key in the retrieved bundle files, using a cached MessageFormat instance per message code.protected String
resolveCodeWithoutArguments(String code, 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).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.void
setFileEncodings(Properties fileEncodings)
Set per-file charsets to use for parsing properties files.void
setPropertiesPersister(PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files.void
setResourceLoader(ResourceLoader resourceLoader)
Set the ResourceLoader to use for loading bundle properties files.String
toString()
从类继承的方法 org.springframework.context.support.AbstractResourceBasedMessageSource
addBasenames, getBasenameSet, getCacheMillis, getDefaultEncoding, getDefaultLocale, isFallbackToSystemLocale, setBasename, setBasenames, setCacheMillis, setCacheSeconds, setDefaultEncoding, setDefaultLocale, setFallbackToSystemLocale
从类继承的方法 org.springframework.context.support.AbstractMessageSource
getCommonMessages, getDefaultMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage
从类继承的方法 org.springframework.context.support.MessageSourceSupport
createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat
构造器详细资料
ReloadableResourceBundleMessageSource
public ReloadableResourceBundleMessageSource()
方法详细资料
setFileEncodings
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.
- 参数:
fileEncodings
- 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".- 另请参阅:
AbstractResourceBasedMessageSource.setBasenames(java.lang.String...)
,PropertiesPersister.load(java.util.Properties, java.io.InputStream)
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".
- 从以下版本开始:
- 4.1
- 另请参阅:
AbstractResourceBasedMessageSource.setCacheSeconds(int)
setPropertiesPersister
public void setPropertiesPersister(@Nullable PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files.The default is a DefaultPropertiesPersister.
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.
- 指定者:
setResourceLoader
在接口中ResourceLoaderAware
- 参数:
resourceLoader
- the ResourceLoader object to be used by this object- 另请参阅:
DefaultResourceLoader
,ResourceLoaderAware
resolveCodeWithoutArguments
protected String resolveCodeWithoutArguments(String code, 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).- 覆盖:
resolveCodeWithoutArguments
在类中AbstractMessageSource
- 参数:
code
- the code of the message to resolvelocale
- the locale to resolve the code for (subclasses are encouraged to support internationalization)- 返回:
- the message String, or
null
if not found - 另请参阅:
AbstractMessageSource.resolveCode(java.lang.String, java.util.Locale)
,MessageFormat
resolveCode
@Nullable protected MessageFormat resolveCode(String code, Locale locale)
Resolves the given message code as key in the retrieved bundle files, using a cached MessageFormat instance per message code.- 指定者:
resolveCode
在类中AbstractMessageSource
- 参数:
code
- the code of the message to resolvelocale
- the locale to resolve the code for (subclasses are encouraged to support internationalization)- 返回:
- the MessageFormat for the message, or
null
if not found - 另请参阅:
AbstractMessageSource.resolveCodeWithoutArguments(String, java.util.Locale)
getMergedProperties
protected ReloadableResourceBundleMessageSource.PropertiesHolder getMergedProperties(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.
calculateAllFilenames
protected List<String> calculateAllFilenames(String basename, Locale locale)
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.- 参数:
basename
- the basename of the bundlelocale
- the locale- 返回:
- the List of filenames to check
- 另请参阅:
AbstractResourceBasedMessageSource.setFallbackToSystemLocale(boolean)
,calculateFilenamesForLocale(java.lang.String, java.util.Locale)
calculateFilenamesForLocale
protected List<String> calculateFilenamesForLocale(String basename, 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()
.- 参数:
basename
- the basename of the bundlelocale
- the locale- 返回:
- the List of filenames to check
getProperties
protected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties(String filename)
Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.- 参数:
filename
- the bundle filename (basename + Locale)- 返回:
- the current PropertiesHolder for the bundle
refreshProperties
protected ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties(String filename, @Nullable ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
Refresh the PropertiesHolder for the given bundle filename. The holder can benull
if not cached before, or a timed-out cache entry (potentially getting re-validated against the current last-modified timestamp).- 参数:
filename
- the bundle filename (basename + Locale)propHolder
- the current PropertiesHolder for the bundle
loadProperties
protected Properties loadProperties(Resource resource, String filename) throws IOException
Load the properties from the given resource.- 参数:
resource
- the resource to load fromfilename
- the original bundle filename (basename + Locale)- 返回:
- the populated Properties instance
- 抛出:
IOException
- if properties loading failed
newProperties
protected Properties newProperties()
Template method for creating a plain newProperties
instance. The default implementation simply callsProperties()
.Allows for returning a custom
Properties
extension in subclasses. Overriding methods should just instantiate a customProperties
subclass, with no further initialization or population to be performed at that point.- 返回:
- a plain Properties instance
- 从以下版本开始:
- 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.- 另请参阅:
clearCache()