Sobes.tech
Senior

How many scopes are there in a Spring application?

sobes.tech AI

Answer from AI

In the Spring Framework, there are several types of scopes for beans that define the lifespan and visibility of instances:

  1. Singleton (default) — one bean instance per Spring container.
  2. Prototype — a new bean instance is created each time it is requested.
  3. Request — the bean lives for the duration of a single HTTP request (for web applications).
  4. Session — the bean lives for the duration of an HTTP session.
  5. Application — the bean lives within the ServletContext (web application).
  6. Websocket — the bean lives for the duration of a WebSocket session.

Thus, in a standard Spring application, 6 main scopes are available. In non-web applications, usually only singleton and prototype are used.