[ACCEPTED]-Mutability and Spring-immutability
In fact, spring beans are immutable by idea, even 13 though you are not enforcing this.
You can 12 provide only a getter to a
final field that is 11 initialized through constructor injection.
Usually 10 you don't do so, but you are never supposed 9 to reassign fields of beans that are injected 8 by the DI framework. That's because spring 7 beans usually do not hold any state, apart 6 from their dependencies (and their scope 5 is singleton). Of course, there are exceptions, like 4 prototype and request scoped beans, that 3 these are rare (for example in 2 big and 2 2 medium projects I've used only 1 prototype-scoped 1 bean)
You can keep classes immutable and still 3 use Dependency Injection if you use constructor-based 2 injection. That way you can avoid unnecessary 1 setters.
I don't see the conflict myself, especially 10 with Spring MVC. Which beans are managed 9 by Spring? Mostly your controllers and in 8 your service/data layer your DAO's and services. These 7 usually have no real state anyway and no 6 setters as well. If your problem lies with 5 the setter injection (for instance, your 4 have your own class that needs to be managed 3 by Spring and you don't want setters for 2 certain fields) then you can use constructor 1 injection instead (or combine both).
More Related questions