Why Java Beans have to be serializable?

可紊 提交于 2019-11-27 21:41:23

问题


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


回答1:


It's one of the "typical" features as described in the Javabeans specification.

Here's an extract of chapter 2.1 What is a bean?

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

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

And here's an extract of chapter 5.5 Summary of Persistence:

All beans must support either Serialization or Externalization.

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.




回答2:


Yes.

By definition - a Java bean is exactly that, a serializable POJO (plain old Java object), with a no-argument constructor and private fields with getters/setters.



来源:https://stackoverflow.com/questions/3142181/why-java-beans-have-to-be-serializable

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