相关文章推荐
好帅的米饭  ·  用 Python ...·  1 年前    · 
逼格高的跑步鞋  ·  c# - If Else in LINQ ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

According to official doc, LocaleContextHolder is:

Simple holder class that associates a LocaleContext instance with the current thread.

So it is tied to current thread , but is this talking about the Thread thread or a thread of current request.

Apologize if it is a dumb question, I am not sure a LocaleContextHolder is tied to a HTTP session or something so that it is safe to use in any service layer class.

If you look at the source code for LocaleContextHolder , you will notice it has a ThreadLocal variable (it has two actually)

private static final ThreadLocal<LocaleContext> localeContextHolder =
        new NamedThreadLocal<LocaleContext>("Locale context");

You can read about what a ThreadLocal is but for our sake, consider it a data structure that maps the ID of the current executing thread to an object of its generic type, LocaleContext here.

A Servlet container has a pool of threads it uses to handle client requests. When a request comes in, it will extract one of these threads and execute your servlet's service() method. With Spring, this results in DispatcherServlet executing and your @Controller's handler method being called. This all happens in that original Thread the servlet container chose.

So when your @Service class' method gets called, you're still in that same thread.

The ThreadLocal in LocaleContextHolder is set() at some point early on in request processing, in FrameworkServlet (which is the parent type of DispatcherServlet) method initContextHolders() called by processRequest() in each of doGet(), doPost(), etc. methods. The Locale is built from the HttpServletRequest with its getLocale() method.

Very good explanations, save me a lot of reading to get such essence. One more question is, as the container holds a pool of thread, one localeContextHolder per thread so does that mean each thread support a DispatchServlet and each dispatchServet holds a localeContexHolder? Thanks in advance. – Dreamer Sep 5, 2013 at 20:22 @Dreamer Not exactly. Each thread will have a LocaleContext (if it is set() on the ThreadLocal). The Thread spawned by the servlet container has a run() method that has a reference to your DispatcherServlet which it uses to call methods with the HttpServletRequest and HttpServletREsponse objects. The DispatcherServlet dispatches to your @Controller methods. The localeContexHolder is a static field of the LocaleContexHolder class. – Sotirios Delimanolis Sep 5, 2013 at 21:07

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.