Class LocaleContextHolder
inheritable flag is set to true.
Note, the inheritable flag relies on JDK
InheritableThreadLocal semantics, which only propagates
the context value to newly created child threads at the time of
their creation. In thread pool environments where threads are reused across
tasks, the context is not refreshed per task submission; reused threads
retain whatever value was inherited when they were originally created.
This mechanism is therefore not suitable for per-task context propagation
in thread pool environments. Instead, consider using a
TaskDecorator that captures the
locale context in the caller thread, applies it around each task execution,
and resets it afterward. The inheritable flag works as expected
with virtual threads, since those are newly created for every task.
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.
- Since:
- 1.2
- Author:
- Juergen Hoeller, Nicholas Williams
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic LocaleReturn the Locale associated with the current thread, if any, or the system default Locale otherwise.static LocalegetLocale(@Nullable LocaleContext localeContext) Return the Locale associated with the given user context, if any, or the system default Locale otherwise.static @Nullable LocaleContextReturn the LocaleContext associated with the current thread, if any.static TimeZoneReturn the TimeZone associated with the current thread, if any, or the system default TimeZone otherwise.static TimeZonegetTimeZone(@Nullable LocaleContext localeContext) Return the TimeZone associated with the given user context, if any, or the system default TimeZone otherwise.static voidReset the LocaleContext for the current thread.static voidsetDefaultLocale(@Nullable Locale locale) Set a shared default locale at the framework level, as an alternative to the JVM-wide default locale.static voidsetDefaultTimeZone(@Nullable TimeZone timeZone) Set a shared default time zone at the framework level, as an alternative to the JVM-wide default time zone.static voidAssociate the given Locale with the current thread, preserving any TimeZone that may have been set already.static voidAssociate the given Locale with the current thread, preserving any TimeZone that may have been set already.static voidsetLocaleContext(@Nullable LocaleContext localeContext) Associate the given LocaleContext with the current thread, not exposing it as inheritable for child threads.static voidsetLocaleContext(@Nullable LocaleContext localeContext, boolean inheritable) Associate the given LocaleContext with the current thread.static voidsetTimeZone(@Nullable TimeZone timeZone) Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.static voidsetTimeZone(@Nullable TimeZone timeZone, boolean inheritable) Associate the given TimeZone with the current thread, preserving any Locale that may have been set already.
-
Method Details
-
resetLocaleContext
public static void resetLocaleContext()Reset the LocaleContext for the current thread. -
setLocaleContext
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.- Parameters:
localeContext- the current LocaleContext, ornullto reset the thread-bound context- See Also:
-
setLocaleContext
Associate the given LocaleContext with the current thread.The given LocaleContext may be a
TimeZoneAwareLocaleContext, containing a locale with associated time zone information.- Parameters:
localeContext- the current LocaleContext, ornullto reset the thread-bound contextinheritable- whether to expose the LocaleContext as inheritable for child threads (using anInheritableThreadLocal); note that this only propagates to newly created threads, not to threads reused in a thread pool (see the class-level note for details)- See Also:
-
getLocaleContext
Return the LocaleContext associated with the current thread, if any.- Returns:
- the current LocaleContext, or
nullif none
-
setLocale
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.
- Parameters:
locale- the current Locale, ornullto reset the locale part of thread-bound context- See Also:
-
setLocale
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.
- Parameters:
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)- See Also:
-
setDefaultLocale
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!
- Parameters:
locale- the default locale (ornullfor none, letting lookups fall back toLocale.getDefault())- Since:
- 4.3.5
- See Also:
-
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()- Returns:
- the current Locale, or the system default Locale if no specific Locale has been associated with the current thread
- See Also:
-
getLocale
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.- Parameters:
localeContext- the user-level locale context to check- Returns:
- the current Locale, or the system default Locale if no specific Locale has been associated with the current thread
- Since:
- 5.0
- See Also:
-
setTimeZone
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.
- Parameters:
timeZone- the current TimeZone, ornullto reset the time zone part of the thread-bound context- See Also:
-
setTimeZone
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.
- Parameters:
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)- See Also:
-
setDefaultTimeZone
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!
- Parameters:
timeZone- the default time zone (ornullfor none, letting lookups fall back toTimeZone.getDefault())- Since:
- 4.3.5
- See Also:
-
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.- Returns:
- the current TimeZone, or the system default TimeZone if no specific TimeZone has been associated with the current thread
- See Also:
-
getTimeZone
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.- Parameters:
localeContext- the user-level locale context to check- Returns:
- the current TimeZone, or the system default TimeZone if no specific TimeZone has been associated with the current thread
- Since:
- 5.0
- See Also:
-