Spring framework does not do anything under the hood concerning the multi-threaded behavior of a singleton bean.
It is the developer’s responsibility to deal with concurrency issue and thread safety of the singleton bean.
While practically, most spring beans have no mutable state, and as such are trivially thread safe. But if your
bean has mutable state, so you need to ensure thread safety. The most easy and obvious solution for this problem is to change
bean scope of mutable beans from “singleton” to “prototype“.
|