How does Spring bean Handle concurrency

前端 未结 4 1706
栀梦
栀梦 2020-12-13 03:16

My web application uses Spring IOC. So all my spring beans will be singletons by default. In case if two requests try to access two different methods of a single class (for

4条回答
  •  悲哀的现实
    2020-12-13 03:46

    As others have already suggested, Spring is going to provide the same instance to all the threads in case of "singleton" beans.

    What you need to understand is that threads do all the work in a system by executing the code while objects provide state and behavior (code). So it is indeed possible for multiple threads (requests in your case), to be concurrently running same methods in a singleton bean. You can either make such beans stateless as Tomasz suggested or otherwise make them "thread-safe".

提交回复
热议问题