类 LocaleContextHolder
- java.lang.Object
- org.springframework.context.i18n.LocaleContextHolder
public final class LocaleContextHolder extends Object
Simple holder class that associates a LocaleContext instance with the current thread. The LocaleContext will be inherited by any child threads spawned by the current thread if theinheritableflag is set totrue.Used as a central holder for the current Locale in Spring, wherever necessary: for example, in MessageSourceAccessor. DispatcherServlet automatically exposes its current Locale here. Other applications can expose theirs too, to make classes like MessageSourceAccessor automatically use that Locale.
- 从以下版本开始:
- 1.2
- 作者:
- Juergen Hoeller, Nicholas Williams
- 另请参阅:
LocaleContext,MessageSourceAccessor,DispatcherServlet
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static LocalegetLocale()Return the Locale associated with the current thread, if any, or the system default Locale otherwise.static LocalegetLocale(LocaleContext localeContext)Return the Locale associated with the given user context, if any, or the system default Locale otherwise.static LocaleContextgetLocaleContext()Return the LocaleContext associated with the current thread, if any.static TimeZonegetTimeZone()Return the TimeZone associated with the current thread, if any, or the system default TimeZone otherwise.static TimeZonegetTimeZone(LocaleContext localeContext)Return the TimeZone associated with the given user context, if any, or the system default TimeZone otherwise.static voidresetLocaleContext()Reset the LocaleContext for the current thread.static voidsetDefaultLocale(Locale locale)Set a shared default locale at the framework level, as an alternative to the JVM-wide default locale.static voidsetDefaultTimeZone(TimeZone timeZone)Set a shared default time zone at the framework level, as an alternative to the JVM-wide default time zone.static voidsetLocale(Locale locale)Associate the given Locale with the current thread, preserving any TimeZone that may have been set already.static voidsetLocale(Locale locale, boolean inheritable)Associate the given Locale with the current thread, preserving any TimeZone that may have been set already.static voidsetLocaleContext(LocaleContext localeContext)Associate the given LocaleContext with the current thread, not exposing it as inheritable for child threads.static voidsetLocaleContext(LocaleContext localeContext, boolean inheritable)Associate the given LocaleContext with the current thread.static voidsetTimeZone(TimeZone timeZone)Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.static voidsetTimeZone(TimeZone timeZone, boolean inheritable)Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.
方法详细资料
resetLocaleContext
public static void resetLocaleContext()
Reset the LocaleContext for the current thread.
setLocaleContext
public static void setLocaleContext(@Nullable LocaleContext localeContext)
Associate the given LocaleContext with the current thread, not exposing it as inheritable for child threads.The given LocaleContext may be a
TimeZoneAwareLocaleContext, containing a locale with associated time zone information.- 参数:
localeContext- the current LocaleContext, ornullto reset the thread-bound context- 另请参阅:
SimpleLocaleContext,SimpleTimeZoneAwareLocaleContext
setLocaleContext
public static void setLocaleContext(@Nullable LocaleContext localeContext, boolean inheritable)
Associate the given LocaleContext with the current thread.The given LocaleContext may be a
TimeZoneAwareLocaleContext, containing a locale with associated time zone information.- 参数:
localeContext- the current LocaleContext, ornullto reset the thread-bound contextinheritable- whether to expose the LocaleContext as inheritable for child threads (using anInheritableThreadLocal)- 另请参阅:
SimpleLocaleContext,SimpleTimeZoneAwareLocaleContext
getLocaleContext
@Nullable public static LocaleContext getLocaleContext()
Return the LocaleContext associated with the current thread, if any.- 返回:
- the current LocaleContext, or
nullif none
setLocale
public static void setLocale(@Nullable Locale locale)
Associate the given Locale with the current thread, preserving any TimeZone that may have been set already.Will implicitly create a LocaleContext for the given Locale, not exposing it as inheritable for child threads.
- 参数:
locale- the current Locale, ornullto reset the locale part of thread-bound context- 另请参阅:
setTimeZone(TimeZone),SimpleLocaleContext(Locale)
setLocale
public static void setLocale(@Nullable Locale locale, boolean inheritable)
Associate the given Locale with the current thread, preserving any TimeZone that may have been set already.Will implicitly create a LocaleContext for the given Locale.
- 参数:
locale- the current Locale, ornullto reset the locale part of thread-bound contextinheritable- whether to expose the LocaleContext as inheritable for child threads (using anInheritableThreadLocal)- 另请参阅:
setTimeZone(TimeZone, boolean),SimpleLocaleContext(Locale)
setDefaultLocale
public static void setDefaultLocale(@Nullable Locale locale)
Set a shared default locale at the framework level, as an alternative to the JVM-wide default locale.NOTE: This can be useful to set an application-level default locale which differs from the JVM-wide default locale. However, this requires each such application to operate against locally deployed Spring Framework jars. Do not deploy Spring as a shared library at the server level in such a scenario!
- 参数:
locale- the default locale (ornullfor none, letting lookups fall back toLocale.getDefault())- 从以下版本开始:
- 4.3.5
- 另请参阅:
getLocale(),Locale.getDefault()
getLocale
public static Locale getLocale()
Return the Locale associated with the current thread, if any, or the system default Locale otherwise. This is effectively a replacement forLocale.getDefault(), able to optionally respect a user-level Locale setting.Note: This method has a fallback to the shared default Locale, either at the framework level or at the JVM-wide system level. If you'd like to check for the raw LocaleContext content (which may indicate no specific locale through
null, usegetLocaleContext()and callLocaleContext.getLocale()- 返回:
- the current Locale, or the system default Locale if no specific Locale has been associated with the current thread
- 另请参阅:
getLocaleContext(),LocaleContext.getLocale(),setDefaultLocale(Locale),Locale.getDefault()
getLocale
public static Locale getLocale(@Nullable LocaleContext localeContext)
Return the Locale associated with the given user context, if any, or the system default Locale otherwise. This is effectively a replacement forLocale.getDefault(), able to optionally respect a user-level Locale setting.- 参数:
localeContext- the user-level locale context to check- 返回:
- the current Locale, or the system default Locale if no specific Locale has been associated with the current thread
- 从以下版本开始:
- 5.0
- 另请参阅:
getLocale(),LocaleContext.getLocale(),setDefaultLocale(Locale),Locale.getDefault()
setTimeZone
public static void setTimeZone(@Nullable TimeZone timeZone)
Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.Will implicitly create a LocaleContext for the given Locale, not exposing it as inheritable for child threads.
- 参数:
timeZone- the current TimeZone, ornullto reset the time zone part of the thread-bound context- 另请参阅:
setLocale(Locale),SimpleTimeZoneAwareLocaleContext(Locale, TimeZone)
setTimeZone
public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable)
Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.Will implicitly create a LocaleContext for the given Locale.
- 参数:
timeZone- the current TimeZone, ornullto reset the time zone part of the thread-bound contextinheritable- whether to expose the LocaleContext as inheritable for child threads (using anInheritableThreadLocal)- 另请参阅:
setLocale(Locale, boolean),SimpleTimeZoneAwareLocaleContext(Locale, TimeZone)
setDefaultTimeZone
public static void setDefaultTimeZone(@Nullable TimeZone timeZone)
Set a shared default time zone at the framework level, as an alternative to the JVM-wide default time zone.NOTE: This can be useful to set an application-level default time zone which differs from the JVM-wide default time zone. However, this requires each such application to operate against locally deployed Spring Framework jars. Do not deploy Spring as a shared library at the server level in such a scenario!
- 参数:
timeZone- the default time zone (ornullfor none, letting lookups fall back toTimeZone.getDefault())- 从以下版本开始:
- 4.3.5
- 另请参阅:
getTimeZone(),TimeZone.getDefault()
getTimeZone
public static TimeZone getTimeZone()
Return the TimeZone associated with the current thread, if any, or the system default TimeZone otherwise. This is effectively a replacement forTimeZone.getDefault(), able to optionally respect a user-level TimeZone setting.Note: This method has a fallback to the shared default TimeZone, either at the framework level or at the JVM-wide system level. If you'd like to check for the raw LocaleContext content (which may indicate no specific time zone through
null, usegetLocaleContext()and callTimeZoneAwareLocaleContext.getTimeZone()after downcasting toTimeZoneAwareLocaleContext.- 返回:
- the current TimeZone, or the system default TimeZone if no specific TimeZone has been associated with the current thread
- 另请参阅:
getLocaleContext(),TimeZoneAwareLocaleContext.getTimeZone(),setDefaultTimeZone(TimeZone),TimeZone.getDefault()
getTimeZone
public static TimeZone getTimeZone(@Nullable LocaleContext localeContext)
Return the TimeZone associated with the given user context, if any, or the system default TimeZone otherwise. This is effectively a replacement forTimeZone.getDefault(), able to optionally respect a user-level TimeZone setting.- 参数:
localeContext- the user-level locale context to check- 返回:
- the current TimeZone, or the system default TimeZone if no specific TimeZone has been associated with the current thread
- 从以下版本开始:
- 5.0
- 另请参阅:
getTimeZone(),TimeZoneAwareLocaleContext.getTimeZone(),setDefaultTimeZone(TimeZone),TimeZone.getDefault()