1

I've already written a couple of Spring Boot application (at the moment, one for web, one using JavaFX and a handful CLI applications). While all work as expected, I currently struggle with one particular concept of desktop or command line applications: The @Scope annotation for @Services and @Components.

I recently read a lot of why singletons are "evil" or at least undesired, but for desktop applications I currently see no other way to implement it, since most of the time a single instance is enough in these kinds of applications.

In Guice I would create an (non-static and non-final) instance in my module. In Spring I use @Scope("singleton").

What I want to know now: Is this a clean solution? Is there any other solution at all?

Regards, Daniel

dzim
  • 1,131
  • 2
  • 12
  • 28
  • 2
    Singletons aren't evil, the singleton pattern is evil but those are different things. Not making things singleton and making everything request/session scoped can even complicate things and make things more memory intensive and less performant. – M. Deinum May 31 '16 at 10:06
  • 1
    To be more precise: the *static singleton pattern* is evil, because it makes code hard to test etc. That doesn't mean that singletons are always evil and to be avoided. There's nothing wrong with Spring singleton beans. – Jesper May 31 '16 at 11:46
  • So to sum it up: I did nothing wrong with annotating the services with the singleton scope. But I would have, if I had used the old singleton pattern. Right? Ok. Maybe then someone could do a "real" answer (from StackOverflow's point of view) and I will vote it up and tick it as correct. Other people stumbling across this will know, what is correct without a need to read each comment. Thanks in advance! – dzim May 31 '16 at 15:58

1 Answers1

2

The articles you're reading are about the Singleton pattern. Many consider Singleton an anti-pattern and there's plenty of information out there on why. See this answer for some good reasons on why you should avoid the pattern.

What you're referring to is singleton as a scope. Spring does not follow the pattern, a scope of singleton simply indicates the container will only create a single instance and use that to satisfy dependencies. There could be multiple containers each with it's own instance, or one container where the bean is singleton scope and another where it's prototype scope instead.

Singleton is the default scope in Spring so you don't actually need to specify it. If you don't have a specific reason to use a different scope then you probably want the default singleton. Sometimes I need a bean to not be shared, in which case I may use prototype. Please check the Spring documentation for more information on available scopes and their meaning.

In any case the key difference here is this is not an implementation of the singleton pattern. If Spring were to implement such a pattern then we would expect every container to have the same instance which is not the case at all.

Community
  • 1
  • 1
kab
  • 131
  • 4
  • As discussed in the earlier comments, I Booth understand and accept this. I was simply unsure, since I kinda got confused by all these articles. Thank you all, for the time you took, to clarify this matter for me! – dzim May 31 '16 at 18:40