What bean scopes does Spr...
What bean scopes does Spr...
Spring supports several bean scopes that determine the lifecycle and visibility of a bean within the application context. Here are the bean scopes supported by Spring:
The singleton scope is the default scope in Spring. When a bean is defined as a singleton, the Spring IoC (Inversion of Control) container creates exactly one instance of the object per Spring IoC container. All subsequent requests and references to that bean will return the shared instance, making it ideal for stateless beans that are common across the application[3][4][5][6][9][10].
In the prototype scope, each request for a bean results in a new instance. This means that if different parts of the application request that bean, each will receive a unique object, which is useful for stateful beans where each instance is expected to have its own state[3][4][5][6][9].
The request scope is only valid within the context of a web-aware Spring ApplicationContext. A new bean instance is created for each HTTP request and is unique to that request. This scope is useful for handling request-specific data and is not shared across different HTTP requests[3][4][5].
Similar to the request scope, the session scope is also only valid in a web-aware Spring ApplicationContext. A single bean instance is created for the duration of an HTTP session. This scope is useful for maintaining user-specific data, like session attributes, across multiple interactions with a web application during the same HTTP session[3][4][5].
The application scope creates a single instance of a bean for the lifecycle of a ServletContext. This is almost similar to the singleton scope but is specific to a web application context. It is useful for creating beans that are shared across all HTTP sessions in a web application[4]...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào