Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why bean class should be serialized

Why should bean classes be serialized?

I have seen bean classes in posts about spring MVC and they are always declared serialized and have a private static final long serialVersionUID. Why?

like image 223
JDevil Avatar asked Mar 17 '23 16:03

JDevil


1 Answers

One good reason, with respect to servlets, is that if you put serializable beans into your web server session, your web server can serialize them to disk if it's shutting down, and then deserialize them back into memory when it starts up. In this way, users wouldn't lose their sessions / logins when the server comes back up.

If there are any non-serializable beans in session, tomcat will fail to serialize sessions to disk.

I believe it's also necessary if you want to sync sessions between multiple servers in a cluster.

like image 94
Neil McGuigan Avatar answered Mar 27 '23 15:03

Neil McGuigan