Why Spring bean is singleton scope?

后端 未结 2 1633
迷失自我
迷失自我 2021-02-06 06:07

I am working with Hibernet and Spring it\'s going good..but I have some doubts

1) why spring scope is singleton by default?Is there any reason for that

2) Can I

2条回答
  •  一向
    一向 (楼主)
    2021-02-06 06:40

    Stateless beans rules :) If you're not going to hold state data in beans then it's enough to have only one instance of each bean. You should also remember that it's not JVM singletons - just Spring singletons. So you don't have to provide only private constructor and any getInstance() methods.

    Quote from Spring documentation:

    When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned.

    Only when you have to keep some session details you should use for example session scope.

提交回复
热议问题