更好地理解错误:当前线程的作用域"会话"不活动

Anv*_*han 2 java spring

我一直在努力理解这个错误.我已经完成了与此相关的大部分问题,我不清楚,因为大多数问题都是如何解决的.我想了解错误是什么以及它为什么会出现.请帮助我理解如下:

  1. 为什么spring会查看范围"session"的当前线程?spring是否在线程上下文中存储了所有请求属性和会话属性?

2.我不一致地接受这个错误.如果这是一个问题,它应该一直都会出现.以下是我的代码段.

- 申请背景

<bean id="location" class="com.mindtree.ruc.cmn.utils.LoginLocation" scope="session">
        <aop:scoped-proxy/>
    </bean>
    <bean id="homePageRH" class="com.rsaame.pas.home.ui.HomePageRH">
        <property name="location" ref="location" />
    </bean>
Run Code Online (Sandbox Code Playgroud)

- web.xml

 <listener>
    <listener-class>
      org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
Run Code Online (Sandbox Code Playgroud)

- 日志:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.rsaame.pas.dao.cmn.PASStoredProcedure]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.location': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    ... 42 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.location': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:341)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget(Cglib2AopProxy.java:653)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:604)
Run Code Online (Sandbox Code Playgroud)

Ral*_*lph 7

问题是会话范围仅在Web上下文中运行的Spring上下文中可用. - 但在普通的Web应用程序中,您有两个Spring上下文!一个由...开始

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

第二个由以下开始:

<servlet>
    <servlet-name>SpringProject-DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)

在第一个上下文中(由ContextLoaderListener它加载)它不在"WebContext"中运行,因此没有会​​话范围!

最简单的解决方案如果可能:将sesson作用域移动到Web上下文(webmvc-config.xml在上面的示例中)


也许这有点帮助:ContextLoaderListener与否?