gpt4 book ai didi

java - 无法理解 : "Scope ' session' is not active for the current thread"

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:29 24 4
gpt4 key购买 nike

我们的项目有批处理,计划在服务器启动时运行,然后每 24 小时运行一次。以下是 spring 文件中的配置。

<bean id="RenewalBatchSvc" class="com.rsaame.pas.renewals.scheduler.RenewalBatchService" >
<property name="renewalBatchSchedulerSvc" ref="RenewalBatchSchedulerSvc" />
</bean>

<bean id="RenewalBatchScheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- wait 20 seconds before starting repeated execution -->
<property name="delay" value="20000" />
<!-- run every 24 hrs 86400000-->
<property name="period" value="86400000" />
<property name="timerTask" ref="RenewalBatchSvc" />
</bean>

<bean id="RenewalBatchTimerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="RenewalBatchScheduledTask" />
</list>
</property>
</bean>

这个批处理过程所做的是,它从批处理表中获取所有要更新的策略,并更新它们。采取每项政策,然后调用更新过程。在更新过程中,我们使用一个名为“location”的 bean,它的作用域是 session 。下面是定义。

<bean id="location" class="com.mindtree.ruc.cmn.utils.LoginLocation" scope="session">
<aop:scoped-proxy/>
</bean>

我们不使用 dispatcherservlet,但我们使用如下定义的上下文监听器(在 web.xml 中):

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

错误:

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)
at com.mindtree.ruc.cmn.utils.LoginLocation$$EnhancerByCGLIB$$db19ad5f.getLocation(<generated>)
at com.mindtree.ruc.cmn.utils.LocationHandler.getLocation(LocationHandler.java:17)
at com.mindtree.ruc.cmn.utils.Utils.getSingleValueAppConfig(Utils.java:707)

问题:

在服务器启动时运行批处理不会出现上述异常。但只有在 24 小时后调用批处理时才会发生。为什么会这样?如果出现异常,那么即使在(IBM websphere)服务器启动时运行批处理,它也应该出现。为什么 session 在服务器启动时可用,甚至在应用程序完全启动并且没有人使用它之前?

在我们将 beans 定义为 session /请求范围的地方,但是我们在服务器启动期间使用这些 beans(还没有 session 或请求),为什么 spring 不会抛出异常?

最佳答案

你的POM有没有:

 <dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>

有很多事情,如果你都做了,请看下面的帖子:

Scope 'session' is not active for the current thread; IllegalStateException: No thread-bound request found

关于java - 无法理解 : "Scope ' session' is not active for the current thread",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28607694/

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