0

PMD spots many Spring components I have, with non-accessable members (no getters or setters), and says the following:

If a class is a bean, or is referenced by a bean directly or indirectly it needs to be serializable. Member variables need to be marked as transient, static, or have accessor methods in the class. Marking variables as transient is the safest and easiest modification. Accessor methods should follow the Java naming conventions, i.e. for a variable named foo, getFoo() and setFoo() accessor methods should be provided.

And I wonder, why does a Spring component/bean has to be Serializable??

Also, is it backed up by Spring documentation (I didn't find...)

Raedwald
  • 46,613
  • 43
  • 151
  • 237
user1028741
  • 2,745
  • 6
  • 34
  • 68
  • 6
    It doesn't have to be. Unless of course you store an instance of such a bean in the session and expects the session to be storable on disk or distributed among nodes in the cluster. – JB Nizet Mar 14 '19 at 12:21
  • @xlecoustillier, it's not a duplicate because I'm talking about Spring Beans and the other thread is talking about Java Beans – user1028741 Mar 14 '19 at 12:43
  • 2
    The quote, and its context, does not mention Spring at all, so it is implicitly talking about *Java* Beans. – Raedwald Mar 14 '19 at 12:49

1 Answers1

0

I don't remeber having written "all" my Spring components as being Serializable, and I am not sure why they should be.

However, data components that can be stored in a database, and components that can be transported on a network must be. It's part of the trade-off for the Spring style Java Beans to be simple POJOs.

In the older days, JEE Java Beans (aka EJB) had to implement many features in order to be managed by the application container. Spring came along, as a light weight app container and changed all that. Spring manages simple POJOs, so I guess having it serializable is not too bad of a trade off.

avi.elkharrat
  • 6,100
  • 6
  • 41
  • 47