Spring MVC thread safe distilled
I come across this question when a controller in Spring MVC which scope is Singleton, is it thread safe?
The answer is NO.
According to java concurrency thread safety is that the shared data, like instance variables and can be modified, then the Singleton Spring MVC controller has the instance variables, or injected autowired Spring bean isn't 100% thread safe. But when member variables, inside the method body, then they are thread safe and no need for synchronised.
Now come along some goodies of the best practice of playing thread safety in Spring by following these simple rules:
1. Don’t use states on Controllers, Service Layer Objects and DAOs
2. If you can’t avoid, first rethink your design, unless you have to use synchronized
3. If you want to use prototype scoped beans, be aware of instance explosion
also externally exposing lists, maps, etc (again via getters ;)...
all of this makes for inherently thread UN-safe code...
Less is more.
Less code is always clean, better and reliable code. Less code reflects clean design and simple approach to solve the complicated problem.
We have data structure but that wouldn't prevent integration architect defined an object in integration layer with hundreds properties. We have the transformer classes which one class could over 1000 lines code threshold just do data mapping. Ironically, IDE automatically generates getters and setters developers have to manually write unit test on getters and setters.
Is this a design issue? I think so ...
The subject of when to use getters and setters came up in one of our Friday Java questions. In a IoC assembled world, Service Layer Objects, Controllers and DAOs only require setters - IMHO having getters only encourages gratuitous violations of the Law of Demeter. DTOs, value objects, entities and the like obviously need getters. Unless required by the framework, setters can be dispensed with in favour of constructor args.
a duckan entity, people want to make it one.Just my 1c :)