Spring session scoped objects in @Scheduled

删除回忆录丶 提交于 2019-12-11 03:54:25

问题


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 scope

  • But 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!