Should service layer classes be singletons?

前端 未结 4 1360
时光说笑
时光说笑 2020-12-13 13:10

I am using Spring framework. Should my service classes be created as singletons? Can someone please explain why or why not? Thanks!

4条回答
  •  温柔的废话
    2020-12-13 14:03

    Yes, they should be of scope singleton. Services should be stateless, and hence they don't need more than one instance.

    Thus defining them in scope singleton would save the time to instantiate and wire them.

    singleton is the default scope in spring, so just leave your bean definitions as they are, without explicitly specifying the scope attribute.

    You can read more about scopes in the spring docs.

提交回复
热议问题