Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java Beans have to be serializable?

Tags:

java

javabeans

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

like image 872
Moritz Avatar asked Jun 29 '10 15:06

Moritz


People also ask

Why Java bean class is should be serializable?

The mechanism that makes persistence possible is called serialization. Object serialization means converting an object into a data stream and writing it to storage. Any applet, application, or tool that uses that bean can then "reconstitute" it by deserialization. The object is then restored to its original state.

Why do we need serializable in Java?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

Are Java Beans serializable?

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.

Do spring beans need to be serializable?

Java Bean is always serializable, Spring Bean doesn't need to. Java Bean must have a default no-arg constructor, Spring Bean doesn't need to.


2 Answers

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.

like image 110
BalusC Avatar answered Oct 12 '22 13:10

BalusC


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.

like image 14
Yuval Adam Avatar answered Oct 12 '22 14:10

Yuval Adam