Sobes.tech
Senior

Why is the prototype scope needed at all? Isn't one singleton enough?

sobes.tech AI

Answer from AI

Prototype scope is used to create a new bean instance each time it is requested from the container, unlike singleton, which is created once and reused.

This is useful when:

  • Objects have state that should not be shared between clients.
  • Multiple independent instances with different parameters need to be created.
  • Objects are temporary or short-lived.

Using singleton for such cases would mean the state is shared, which can lead to errors and undesirable behavior.

For example: if you have a bean representing a user session, then each user needs a separate instance (prototype), not a shared one (singleton).