What are different Bean Scopes in Spring?

Carvia Tech | May 05, 2019 | 1 min read | 28 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. Run method on application startup in Spring Boot
  3. Spring Boot 2.0 Reactive Web Performance Metrics
  4. How will you handle unresolved circular dependency in spring dependency injection?
  5. What is new in Spring Boot 2
  6. How to prevent duplicate form submission in Spring MVC
  7. Setting a Random Port in Spring Boot Application at startup



Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info