Câu hỏi phỏng vấn Spring
Câu hỏi

What bean scopes does Spr...

Câu trả lời

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:

Singleton Scope

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].

Prototype Scope

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].

Request Scope

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].

Session Scope

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].

Application Scope

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

senior

Gợi ý câu hỏi phỏng vấn

middle

Is the DispatcherServlet instantiated via an application context?

middle

How to validate form data in Spring Web MVC Framework?

middle

How do you define a bean scope?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào