Sobes.tech
Middle — Middle+

Is it possible for a single Singleton bean to have multiple instances within the same context?

sobes.tech AI

Answer from AI

Within a single Spring ApplicationContext, a Singleton bean by definition exists as a single instance. That is, when requesting this bean from the context, the same object is always returned.

However, there are situations where it may seem that a Singleton bean has multiple instances:

  • If multiple contexts are used (for example, parent and child), each context will have its own instance of the Singleton bean.
  • When using proxies or factories that create new objects inside the Singleton bean.
  • If the bean is declared with a different scope (for example, prototype), but injected into a Singleton bean without proper handling.

Thus, in one context, a Singleton bean always has one instance, but in different contexts, there can be several.

Is it possible for a single Singleton bean to have… - sobes.tech