Reputation: 61
In a recent Web based Java project, I have come across various places where I have seen a Spring based Controller/Component implementing Serializable
.
I am curious, as to why a Controller/Component should have to implement Serializable
?
I think it is wrong practice to do so and searching so far for any scenario to implement Serializable
has not yielded anything. I think it violates MVC principle and is unconventional. You would want to keep Domain/Business Objects separate from Controllers/Components.
I would like to correct this bad practice in the code base but just wanted to ensure I have a reason to do so.
Any explanation/scenario illustrating this case will be appreciated.
Upvotes: 6
Views: 5065
Reputation: 432
A specific use case that requires the Controller classes to implement Serializable is when working with spring session.
Spring session will externalize the session - I use spring session with JDBC to co-ordinate the session between containers. When you have a Session Scoped Controller spring session will serialize that state to and from the database using a byte[] column.
Upvotes: 0
Reputation: 23044
There's no need for controllers to be Serializable
. They're stateless and just mediate between the model and the view. The same could be said for other stateless components. There's no benefit in making them Serializable
.
Stateful model objects, however, are often Serializable
because they're sometimes placed in HttpSession
. App servers require objects in session to be Serializable
for replication and persistence purposes.
Upvotes: 3