Java – immutable objects and spring / sring MVC: the right choice?
I usually try to design my courses as immutable classes, so I have many advantages in programming stress
However, when working with spring, I sometimes notice that in most cases, the framework does not encourage this design and supports the classic JavaBeans design: the default constructor getter / setter
I really don't like JavaBean designed objects because of their crazy variability So I want to know if I'm missing something
I try to make my classroom design as elegant and reusable as possible, but the framework needs to change this design or allow it, but in a difficult way
What's going on?
Solution
For web form data binding (i.e. formal post), the problem is that java reflection is very weak on the constructor, so it is difficult to bind data without comments A long time ago, I considered submitting a bug. Spring data binding should take advantage of the forgotten @ constructorproperties (IIRC I investigated the patch myself, but it's very complex and will break a lot of things) Someone should be able to submit a function request
BTW I'm talking about web data binding (not dependency injection), because spring has supported constructor based Di for a long time (immutable objects need constructor based injection) In fact, I would say that constructor based injection or (static method factory) is becoming the preferred method for traditional getter / setter components (you can see that this has changed to final and constructor in many spring classes for many years)
Anyway, I can use Jackson https://gist.github.com/agentgt/4458079 Web data binding to immutable objects
(although it uses Jackson to bind data, the request is not necessarily JSON)
You may also want to see spring webflow databinding to immutable objects via a constructor? I originally thought of the idea and have more information