Junior — Middle
What is Scope in the context of beans and what types of Scope exist?
sobes.tech AI
Answer from AI
In the context of the Spring Framework, Scope (lifetime scope) defines the lifecycle and visibility of a bean (object) managed by the container.
Main types of Scope:
- singleton — one instance of the bean for the entire Spring container. This is the default value.
- prototype — each request to the container creates a new instance of the bean.
- request — the bean lives for the duration of a single HTTP request (used in web applications).
- session — the bean lives within the user's HTTP session.
- application — the bean lives within the ServletContext, i.e., at the level of the entire web application.
- websocket — the bean lives within a WebSocket session.
The choice of Scope depends on how long the object should exist and in what context it is used.