Sobes.tech
Junior — Middle

What types of scope exist for components or beans in dependency injection?

sobes.tech AI

Answer from AI

In the context of Dependency Injection in Java, the following scope types for components or beans are usually distinguished:

  • Singleton — one instance for the entire lifecycle of the application. All requests receive the same object.
  • Prototype — a new instance is created for each request.
  • Request — one instance per HTTP request (relevant for web applications).
  • Session — one instance per user session.
  • Application — one instance for the entire web application (similar to singleton, but in the web context).

In Spring, for example, these scopes are set using annotations or configuration and allow managing the lifespan and visibility of beans.