Junior — Middle
How do different types of Scope in Spring beans differ?
sobes.tech AI
Answer from AI
In Spring, there are different types of scope (lifetime) for beans, which determine the lifecycle and visibility of instances:
- singleton (default): one bean instance is created for the entire Spring container. All requests for this bean receive the same object.
- prototype: a new bean instance is created for each request.
- request: the bean lives within a single HTTP request (for web applications).
- session: the bean lives within the user's HTTP session.
- application: the bean lives within the ServletContext (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.