Senior
What scopes do Spring beans have?
sobes.tech AI
Answer from AI
In Spring, the following main scopes for beans exist:
- singleton (default) — one instance of the bean for the entire Spring container.
- prototype — each request to the container creates a new instance of the bean.
- request — the bean lives within a single HTTP request (applicable 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.
Example of a declaration with scope prototype:
@Component
@Scope("prototype")
public class MyBean {
// ...
}