Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...