How do Spring singleton beans work?

China☆狼群 提交于 2020-01-05 05:17:13

问题


We have a backend application with Spring IOC with Jersey REST. All the spring beans we define use default scope, I mean, singleton.

This application is DB-centric with huge number of users. My question: how spring singleton beans manage the concurrency over this application? None of the methods in service classes are defined as synchronized, but we never get any issues with user data synchronization. Everything looks perfectly OK.

I think I am missing something about the spring singleton beans or how spring handles multiple requests with singleton object.

As per my understanding as spring beans are singletons and no service methods are defined as synchronized, there must be some synchronization issues with multiple requests.

Can you help me understand this thing properly?


回答1:


Spring singletons are in singleton in the scope of spring container.

And if you didn't have any problem so far because of your implementation. When your beans are stateless there is no concern about concurrency.

Concurrency concerns are important when you have statefull beans and you are changing the state.When bean methods have own inputs from function aurguments, there is no concern anymore



来源:https://stackoverflow.com/questions/39281680/how-do-spring-singleton-beans-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!