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

时间:2014-12-01 11:17:58

标签: java spring

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

  1. 为什么spring会查看范围的当前线程" session" ? Spring是否在线程上下文中存储了所有请求属性和会话属性?
  2. 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>
    

    - web.xml

     <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
    

    - 日志:

    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)
    

2 个答案:

答案 0 :(得分: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>

和第二个由:

开始
<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>

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

最简单的解决方案如果可能:将该范围内的sesson移至网络上下文(上例中的webmvc-config.xml


也许这有点帮助:ContextLoaderListener or not?

答案 1 :(得分:2)

您的第一个问题的答案在于您的配置本身。 bean范围定义为session,您也指定了<aop:scoped-proxy/>。这意味着该特定bean将在 mandatorily 具有与之关联的HTTP会话的线程中使用。如果没有会话可用,则会得到上述错误。

对第二个问题的回答是,您似乎从至少另外两个bean中引用了这个bean;一个具有与之关联的HTTP会话而另一个没有与之相关联的会话。因此问题是间歇性的。

要摆脱它,你需要分析你的bean定义并确保使用/注入有问题的bean,以便在调用时可以使用HTTP会话。