Java – Using spring to Avoid Singleton pattern

javasingletonspring

Considering that Spring framework can manage pretty everything related to object creation (life cycle, object scope ..), is it longer necessary to use singleton pattern in an application using the framework, or are there still situations when we need to use Singleton pattern instead of singleton instance created by Spring?

One of the problem of using the singleton pattern is global state. Is the problem avoided using the Spring-defined singleton scope?

Best Answer

"One of the problem of using the singleton pattern is global state"

If you use it for global state then you can do so. But that does not affect the "singleton pattern" itself as it is answering the question how many instances of an object there may be: One.

"global" and "state" do not adress the question "How many instances may be there?". They adress the questions "What is the scope?" and "May data change?" (mutability).

Spring will manage singletons for you. But the scope and mutability is up to you to define.