本文介绍了将会话同步添加到Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将会话同步添加到Spring MVC应用程序.我正在使用Spring 3.1.2.

I need to add session synchronization to a Spring MVC application. I'm using Spring 3.1.2.

需要同步,因为我将有多个AJAX调用更新会话数据.并非在Web应用程序中同步会话数据,而是做出了设计决定,以强制同步处理传入的呼叫.

The synchronization is required because I will have multiple AJAX calls updating session data. Rather than synchronize the session data within the web app, the design decision was made to force the incoming calls to be processed synchronously.

在我的servlet.xml中,我具有以下内容:

In my servlet.xml, I have the following:

<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="synchronizeOnSession" value="true" />
</bean>

但是,如果我从同一会话对同一请求处理程序进行两次AJAX调用,它们将同时执行.我期待的是syncnizeOnSession可以防止这种情况.

Yet if I do two AJAX calls from the same session to the same request handler, they both will execute at the same time. I was expecting the synchronizeOnSession to prevent this.

我在配置中缺少什么吗?

Am I missing something in the configuration?

推荐答案

我最终添加了一个BeanPostProcessor来设置syncnizeOnSession标志.

I ended up adding a BeanPostProcessor to set the synchronizeOnSession flag.

添加了这个新类:

@Component
public class MyPostProcessor implements BeanPostProcessor
{
  @Override
  public Object postProcessBeforeInitialization( Object bean, String name ) throws BeansException
  {
    if( bean instanceof RequestMappingHandlerAdapter )
    {
      RequestMappingHandlerAdapter adapter = ( RequestMappingHandlerAdapter ) bean;
      adapter.setSynchronizeOnSession( true );
    }

    return bean;
  }

  @Override
  public Object postProcessAfterInitialization( Object bean, String beanName ) throws BeansException
  {
    return bean;
  }
}

在应用程序上下文中连同组件扫描条目:

Along with a component scan entry in the application context:

<context:component-scan base-package="com.company.base.spring.MyPostProcessor" />

您可以在handleInternal(...)的RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation)中设置一个断点,以确认将syncnizeOnSession设置为true.

You can set a breakpoint in the RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation) at handleInternal(...) to confirm that the synchronizeOnSession is set to true.

这篇关于将会话同步添加到Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:19