Sobes.tech
Junior — Middle

Can more than one Singleton instance be implemented within a Spring application?

sobes.tech AI

Answer from AI

By default, in a Spring application, each bean with singleton scope is created as a single instance for the entire application context. However, it is possible to have multiple different singleton beans — one instance of each class.

If the question is about multiple instances of the same singleton bean, then by default this is not possible, as Spring guarantees a single instance for each beanId.

To create multiple instances of the same class, you need to use a different scope, such as prototype, or create beans with different names and configurations.

Thus, in one application, there can be many singleton beans of different classes, but only one singleton instance per class by default.