1

In our web application, we are using Spring as dependency injection mechanism. We have multiple DAOs and Manager spring beans which has a default scope of singleton.We have made sure that all of these beans are stateless. I have read at multiple places why singleton pattern is bad : What is so bad about singletons? .Forgive me if this is asked already but please answer if the above mentioned design comes under bad coding practice and also how improvements can be made to same.

Community
  • 1
  • 1
Abhishek Garg
  • 2,158
  • 1
  • 16
  • 30

4 Answers4

3

As you can see in the first response to the link you posted (what is so bad about singleton) three problems of singleton are:

  • Hide dependencies of the application in the code
  • Violation of single responsible principle
  • Code strong coupled

None of them is present if you use the singleton via spring dependency injection, because:

  • Dependencies are stored in the configuration file, not in the code
  • The single responsible principle is not broken because the construction of your dao is handled by the spring engine, not by your dao
  • Code is not strong coupled because the configuration can be changed without changing the code

So yes using spring singletons can be a very nice solution not having problems of real singletons.

Community
  • 1
  • 1
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Thanks Davide for the crisp answer. If it is not too much trouble can you please explain "hide the dependencies of your application in your code, instead of exposing them through the interfaces" from the original singleton are bad link. I dont understand if I am creating a singleton how am I hiding the dependencies as I still need to inject them in singleton. – Abhishek Garg Jan 09 '16 at 17:08
  • Basically if you create the singleton in your class you tie the concrete singleton class to the class using it. Instead if you define only a reference to the singleton as type of its superinterface you can change the instance as you like (for example in tests you can easily mock it). – Davide Lorenzo MARINO Jan 10 '16 at 09:24
2

Spring singleton beans are not using the singleton pattern. They just happen to be instantiated once by the framework, and injected into every component that needs them.

The singleton pattern is considered an anti-pattern mainly because it makes all the classes relying on it hard to test. Spring singleton beans don't have this problem at all.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

The primary disadvantage of singletons is that they tend to cause entanglements between different client classes if they have any state. Well-designed service classes should not have any operation-specific state (they'll usually have something like a reference to a database pool or other resources, injected at instantiation and then not changed after), and it's perfectly fine to use a singleton-scoped bean for such services.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

If you read the answers from the question you've linked, they don't really apply to Spring singletons. When using Spring beans in singleton scope they're not hard to test, they do not control their own creation, and they are accessed through an interface (or at least should be!).

Kayaman
  • 72,141
  • 5
  • 83
  • 121