St.Antario
St.Antario

Reputation: 27435

Understanding Java singleton Design pattern

During reading the Spring Framework's documentation I came across the following:

Spring’s concept of a singleton bean differs from the Singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF Singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean.

I don't understand why per ClassLoader? Why don;t per the entire application or in the Context they are considered to be the same things?

Upvotes: 2

Views: 112

Answers (1)

Kayaman
Kayaman

Reputation: 73568

Because in the traditional singleton, you have a static variable in the Singleton class to keep the single instance. However since you can load the same class using multiple ClassLoaders, those will have their own static variables and are free to create their own instance.

In most cases this is not a problem anyway.

Upvotes: 2

Related Questions