spring mvc declaring all beans singleton

前端 未结 2 1628
失恋的感觉
失恋的感觉 2021-01-01 03:00

I have this new mvc project where all beans are default scoped(no prototype or session). with single application context.

i want to know by making all beans to b

相关标签:
2条回答
  • 2021-01-01 03:18

    Singleton means that the there will be only one instance of each bean. Generally, such beans are processing elements that carry no state. The methods called on them are passed the context which contains the inputs to work on. Hence the method calls on such singleton beans are inherently thread-safe.

    0 讨论(0)
  • 2021-01-01 03:19

    Because Spring beans are typically stateless, you can safely call them from multiple threads. That's how your application works: there is only one instance of every controller, service, DAO, etc. But your servlet container (through Spring) calls these beans from multiple threads - and it's completely thread safe.

    In fact in plain servlets the situation is the same - there is only instance of each servlet and it can be accessed by infinite number of threads. As long as this servlet is stateless or properly synchronized.

    Do not confuse Spring with stateless session beans in ejb that are pooled and each client gets its own instance from the pool.1

    1 - In fact that's a bit dumb - since the beans are stateless by the definition, there is no point in pooling them and preventing concurrent access...

    0 讨论(0)
提交回复
热议问题