@Scheduled中的Spring session作用域对象 [英] Spring session scoped objects in @Scheduled
问题描述
在春季,我有很多代码使用如下定义的会话bean:
In spring, I have a lot of code that uses session beans defined like this:
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
-
在我的Web应用程序中,一切正常,因为会话范围是 可用.
In my webapplication all is fine, since a session scope is available.
在我的JUnit测试中,由于我使用的是
WebContextTestExecutionListener
(In my JUnit tests, all is also fine since i'm using a
WebContextTestExecutionListener
(link) that registers a thread scope for the session scope但是当调用具有
@Scheduled
的方法时,由于没有 会话范围.But when a method with
@Scheduled
is called, I get an exception since there is no session scope.现在我的问题是:如何在我的
@Scheduled
方法中为会话范围注册线程范围?Now my question is: How can I register a thread scope for the session scope in my
@Scheduled
method?我已经尝试过类似的操作:
beanFactory.registerScope("session", new SimpleThreadScope());
,但这也覆盖了我的Web应用程序的会话范围:(I have tried something like this:
beanFactory.registerScope("session", new SimpleThreadScope());
but that also overrides the session scope of my webapplication :(推荐答案
It turned out, this question is very much related to: spring 3 scheduled task running 3 times. Since my ContextLoaderListener and DispatcherServlet were pointing at the same context config, the scopes got overridden. @skaffman/Wesley: Thanks for your comments.
这篇关于@Scheduled中的Spring session作用域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!