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:
  1. Spring DI - Singleton beans with prototype-bean dependencies
  2. What is difference between Component, Repository, Service, Controller & RestController
  3. Testing web layer in Spring Boot using WebMvcTest
  4. What are inheritance mapping strategies in JPA
  5. Prevent Lost Updates in Database Transaction using Spring Hibernate
  6. Spring Boot WebClient Basic Authentication
  7. What is new in Spring Boot 2

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.