While designing a service class should it be singleton in java? Generally DAO is made singleton, so should the calling Service class be made singleton also?
In this case, service is a non-singleton nature. It will create multiple instances of a service. Every time a new instance of provided service will be created when a component is used inside another component. Service is being destroyed along with the component.
A singleton service is a service for which only one instance exists in an application. For a sample application using the app-wide singleton service that this page describes, see the live example / download example showcasing all the documented features of NgModules.
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.
A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.
IMHO yes, services should not hold state and should therefore be made singleton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With