为什么Java Bean必须可序列化? [英] Why Java Beans have to be serializable?

查看:492
本文介绍了为什么Java Bean必须可序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Bean是否需要实现 Serializable 接口?

Is it necessary that a Java Bean implements the Serializable interface?

推荐答案

这是中描述的典型功能之一Javabeans规范

这是章节的摘录 2.1什么是豆?


单个Java Bean的功能会有所不同,但区分Java Bean的典型统一功能
是:

Individual Java Beans will vary in the functionality they support, but the typical unifying features that distinguish a Java Bean are:


  • 支持内省,以便构建器工具可以分析bean的工作方式

  • 支持自定义,以便在使用应用程序构建器时,用户可以
    自定义bean的外观和行为。

  • 支持事件作为简单的通信隐喻,而不是用于连接
    up beans。

  • 支持属性,包括自定义和程序化使用。

  • 支持持久性,以便可以在应用程序构建器中自定义bean,然后
    将其自定义状态保存起来并稍后重新加载。

  • Support for "introspection" so that a builder tool can analyze how a bean works
  • Support for "customization" so that when using an application builder a user can customize the appearance and behaviour of a bean.
  • Support for "events" as a simple communication metaphor than can be used to connect up beans.
  • Support for "properties", both for customization and for programmatic use.
  • Support for persistence, so that a bean can be customized in an application builder and then have its customized state saved away and reloaded later.

这是一个章节摘要 5.5持久性摘要


所有bean必须支持序列化或外化。

All beans must support either Serialization or Externalization.

实际上,它没有明确的功能。一般情况下,如果不实施 序列化 。无论何时你想将它们普通存储在硬盘上或通过网络发送普通,它都会很有用。例如,当它是一个会话范围的bean,它将存储在HTTP会话中,并且服务器已经被配置为在关闭/重启期间保持并恢复HTTP会话。无论何时,只要您面对 NotSerializableException 消息中包含bean的完全限定类名,然后它就足以让它实现 Serializable

In practice, it's not explicitly necessary for it to function. It will in general also just work fine without implementing Serializable. It's however useful whenever you'd like to store them "plain" on harddisk or send "plain" over network. For example when it's a session scoped bean which is to be stored in the HTTP session and the server is been confugured to persist and revive HTTP sessions during shutdown/restart. At any way, whenever you face a NotSerializableException with the bean's full qualified classname in the message, then it's enough sign to let it implement Serializable.

这篇关于为什么Java Bean必须可序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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