gpt4 book ai didi

java - 更好地理解错误 : Scope 'session' is not active for the current thread

转载 作者:行者123 更新时间:2023-12-02 08:21:20 25 4
gpt4 key购买 nike

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

  1. 为什么 spring 在当前线程中查找作用域“session”? spring是否将所有请求属性和 session 属性存储在线程上下文中?

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)

最佳答案

问题是 session 范围仅在 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”中运行,因此没有会​​话范围!

最简单的解决方案如果可能:将 session 范围移至 Web 上下文(上例中的 webmvc-config.xml)

<小时/>

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

关于java - 更好地理解错误 : Scope 'session' is not active for the current thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27227233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com