What are different Bean Scopes in Spring?
Carvia Tech | 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
Find more on this topic:
Subscribe to Interview Questions
Recommended books for interview preparation:
Similar Posts
- Elasticsearch with Spring Boot + Spring Data
- Download a file using Spring RestTemplate
- Slack Webhook Message from Spring Boot
- Spring Webclient multipart file upload
- Dialoglfow fulfillment with Spring Boot
- Spring Boot WebClient Basic Authentication
- SendGrid Attachments with Spring Boot
- Sendgrid Dynamic Templates with Spring Boot
- Redis rate limiter in Spring Boot
- Custom TTL for Spring data Redis Cache