What are different Bean Scopes in Spring?
Upasana | May 05, 2019 | 1 min read | 166 views
Spring Framework support total seven scopes, out of which five are available only if you use a web-aware ApplicationContext
.
- singleton
-
This is the default scope for any bean. It created a single object instance for a single bean definition per Spring IoC container.
- prototype
-
Any number of object instances can be created for a single bean definition. Everytime we request a bean using
getBean()
method call on the IoC container, a new instance will be returned.
web-aware Beans Scopes are:
- request
-
Each HTTP request will create its own instance of bean from a single bean definition.
- session
-
Scopes a single bean definition to the lifecycle of an HTTP session.
- globalSession
-
Scopes a single bean definition to the lifecycle of an global HTTP session. Only valid for Portlet context.
- application
-
Scopes a single bean definition to the lifecycle of a
ServletContext
. - websocket
-
Scopes a single bean definition to the lifecycle of a
WebSocket
.
Top articles in this category:
- Spring DI - Singleton beans with prototype-bean dependencies
- What is difference between Component, Repository, Service, Controller & RestController
- Testing web layer in Spring Boot using WebMvcTest
- What are inheritance mapping strategies in JPA
- Prevent Lost Updates in Database Transaction using Spring Hibernate
- Spring Boot WebClient Basic Authentication
- What is new in Spring Boot 2