Spring 会话范围的 bean(控制器)和对服务的引用,就序列化而言 [英] Spring session-scoped beans (controllers) and references to services, in terms of serialization

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

问题描述

  • 一个标准案例 - 你有一个带有 @Scope("session") 的控制器 (@Controller).
  • 放在会话中的类通常应该实现Serializable,以便它们可以在服务器重启时物理存储,例如
  • 如果控制器实现了Serializable,这意味着它引用的所有服务(其他 spring bean)也将被序列化.它们通常是代理,引用事务管理器、实体管理器工厂等.
  • 通过实现ApplicationContextAware,某些服务甚至控制器持有对ApplicationContext 的引用并非不可能,因此这可以有效地意味着整个上下文是连载.并且鉴于它拥有许多连接 - 即无法通过想法序列化的事物,它将在损坏状态下恢复.
  • 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 并通过静态实用程序类 WebApplicationContextUtils 将它们返回到 readResolve() 中,这样就可以保存ThreadLocal 中的 request/ServletContext.这很乏味,但它保证,当对象被反序列化时,它的依赖项将与当前应用程序上下文保持同步".

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.

对此是否有任何公认的做法,或任何有关序列化 spring 上下文部分的指南.

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.

推荐答案

this演示(大约 1:14)演讲者说这个问题在 spring 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(控制器)和对服务的引用,就序列化而言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆