Spring会话范围的bean

Spring会话范围的bean

本文介绍了在序列化方面,Spring会话范围的bean(控制器)和对服务的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 一个标准情况 - 你有一个控制器( @Controller ), @Scope(session)

  • 通常会在会话中放置的类实现 Serializable ,以便它们可以物理存储重启服务器的情况,例如

  • 如果控制器实现 Serializable ,这意味着它所指的所有服务(其他spring bean)也将被序列化。它们通常是代理,引用了交易管理器,实体管理器工厂等。

  • 某些服务甚至控制器都不可能拥有对 ApplicationContext ,通过实现 ApplicationContextAware ,这样就可以有效地表示整个上下文是序列化的。并且考虑到它拥有许多连接 - 即不能通过构思序列化的东西,它将在腐败状态下恢复。

  • a standard case - you have a controller (@Controller) with @Scope("session").
  • classes put in the session usually are expected to implement Serializable so that they can be stored physically in case the server is restarted, for example
  • If the controller implements Serializable, this means all services (other spring beans) it is referring will also be serialized. They are often proxies, with references to transaction mangers, entity manager factories, etc.
  • It is not unlikely that some service, or even controller, hold a reference to the ApplicationContext, by implementing ApplicationContextAware, so this can effectively mean that the whole context is serialized. And given that it holds many connections - i.e. things that are not serializable by idea, it will be restored in corrupt state.

到目前为止我大多忽略了这些问题。最近我想到声明我所有的spring依赖 transient 并通过静态实用程序类将它们返回到 readResolve() code> WebApplicationContextUtils 等将请求/ ServletContext保存在 ThreadLocal 中。这很乏味,但它保证了,当对象被反序列化时,它的依赖关系将是当前应用程序上下文的最新。

So far I've mostly ignored these issues. Recently I thought of declaring all my spring dependencies transient and getting them back in readResolve() by the static utility classes WebApplicationContextUtils and such that hold the request/ServletContext in a ThreadLocal. This is tedious, but it guarantees that, when the object is deserialized, its dependencies will be "up to date" with the current application context.

是否有任何公认的做法,或任何序列化弹簧上下文部分的指南。

Is there any accepted practice for this, or any guidelines for serializing parts of the spring context.

请注意,在JSF中,托管bean(〜控制器)是有状态的(不像基于动作的Web框架)。所以也许我的问题更多的是JSF,而不是spring-mvc。

Note that in JSF, managed beans (~controllers) are stateful (unlike action-based web frameworks). So perhaps my question stands more for JSF, than for spring-mvc.

推荐答案

在(大约1:14)发言人说这个问题在春季3.0通过提供代理解决了不可序列化的bean,它从当前应用程序上下文中获取实例(在反序列化时)

In this presentation (around 1:14) the speaker says that this issue is resolved in spring 3.0 by providing a proxy of non-serializable beans, which obtains an instance from the current application context (on deserialization)

这篇关于在序列化方面,Spring会话范围的bean(控制器)和对服务的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:16