问题
In spring, I have a lot of code that uses session beans defined like this:
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
In my webapplication all is fine, since a session scope is available.
In my JUnit tests, all is also fine since i'm using a
WebContextTestExecutionListener
(link) that registers a thread scope for the session scopeBut when a method with
@Scheduled
is called, I get an exception since there is no session scope.
Now my question is: How can I register a thread scope for the session scope in my @Scheduled
method?
I have tried something like this: beanFactory.registerScope("session", new SimpleThreadScope());
but that also overrides the session scope of my webapplication :(
回答1:
Scheduled tasks have nothing to do with the sessionscope, the session may be even terminated by the time the scheduled task get executed. If you scheduled task requires data from the session, just pass a new object containing the data to the scheduled method.
回答2:
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.
来源:https://stackoverflow.com/questions/9549909/spring-session-scoped-objects-in-scheduled