Announcement Announcement Module
Collapse
No announcement yet.
Request scope - help needed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Request scope - help needed

    I am very confused how to implement some things in Spring Web MVC Framework, which require changes in behavior depending on HttpServletRequest.

    When I wire all beans in .xml file, I cannot change their behavior upon each "request".

    For example, I am struggling right now to setup Velocity so that it would render templates depending on the current locale. So, I'm thinking about some sort of LocaleResourceLoader for Velocity and I'd wire the beans like this (simplified code, not real here):

    Code:
    <bean id="engine" class="...VelocityEngine">
      <property name="resourceLoader"><ref bean="localeResourceLoader" /></property>
    </bean>
    
    <bean id="localeResourceLoader" class="....LocaleResourceLoader">
       <property name="locale">?????</property>
    </bean>
    The thing is, I cannot specify concrete locale in "localeResourceLoader" bean, because I don't know it yet, it will be known only in Request Scope, not Web App Scope.

    Any ideas/suggestions would be greatly appreciated, since this is very confusing to a newbie like me.

  • #2
    If I was doing it without Spring, I'd code something simple like this:

    Code:
    public void handle&#40;request, response&#41; &#123;
    // ...
    localeResourceLoader.setLocale&#40;localeResolver.resolve&#40;request&#41;&#41;;
    // ...
    &#125;
    But what I'd like to know is: what are the best practices in Spring working with beans which require "Request Scope" tweaking.

    Comment


    • #3
      I guess I figured it out on my own - the possible solution is to use Interceptors functionality which is provided by Spring.

      I might use interceptor to set all request scope configuration required for work of application.

      Comment


      • #4
        I realized that the solution I found - setting locale in interceptor like this is VERY BAD:

        Code:
        public class LocaleSetterInterceptor extends HandlerInterceptorAdapter &#123;
            private LocaleAware bean;
        
            public LocaleAware getBean&#40;&#41; &#123;
                return bean;
            &#125;
        
            public void setBean&#40;LocaleAware bean&#41; &#123;
                this.bean = bean;
            &#125;
        
            public boolean preHandle&#40;HttpServletRequest request, HttpServletResponse response, Object o&#41; throws Exception &#123;
                Locale locale = RequestContextUtils.getLocale&#40;request&#41;;
                bean.setLocale&#40;locale&#41;;
                return true;
            &#125;
        &#125;
        At first glance, it looks like a good solution, however the problem is that this code is not thread-safe. Since "bean" of LocaleAware type is "singleton" here, it is shared between all threads serving website visitors.

        This means that if one user requested a webpage in Japanese, and another user requested the same page in English, first user can incidentially receive the page in English.

        How can I ensure thread-safety of changing locales?

        Comment

        Working...
        X