Adding session synchronization to Spring MVC

二次信任 提交于 2019-12-04 15:13:06

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

Added this new class:

@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" />

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!