Spring bean singleton bean: multi thread access

2019-09-20 03:39发布

问题:

Singleton beans by design pattern should be of single instance.
Hence, if a thread's execution is on the way of execution blocking a series of beans (say @Controller then @Service then simple beans) one after another

  • What will happen if many other simultaneous threads having different functionality but using the same series of beans runs - I mean any conflict situation with data/value may arise or Spring Singleton is thread safe ?

How to resolve the same if not ?

回答1:

Controllers, as singleton, aren't thread safety.

I think your singleton beans should be stateless.

But you can inject other request or session scoped beans using proxyMode, for instance http://healthycoder.in/session-scoped-bean-in-a-controller-spring-mvc/