What bean scopes does Spring support? Explain them.
What bean scopes does Spring support? Explain them.
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.
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.
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.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.