Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a Service class be singleton in java?

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?

like image 690
Jyotirup Avatar asked Dec 29 '11 16:12

Jyotirup


People also ask

Is service a class singleton?

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.

Why @service is singleton?

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.

Are spring @service singletons?

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.

When should a class be singleton?

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.


1 Answers

IMHO yes, services should not hold state and should therefore be made singleton.

like image 184
Stijn Geukens Avatar answered Oct 18 '22 22:10

Stijn Geukens