What are different Spring Bean Scopes?

Category : Interview Questions | Sub Category : Spring Boot Interview Questions | By Prasad Bonam Last updated: 2023-08-14 11:23:00 Viewed : 281


In the Spring Framework, bean scope defines the lifecycle and visibility of a bean instance within the application context. Different scopes are used to control how Spring manages the lifecycle of beans and their instances. The following are the different bean scopes available in Spring:

  1. Singleton Scope (default):

    • The default scope.
    • Only one instance of the bean is created per Spring container.
    • The same instance is returned whenever the bean is requested from the container.
    • Suitable for stateless beans and beans that are thread-safe.
  2. Prototype Scope:

    • A new instance of the bean is created each time it is requested from the container.
    • Can result in more memory usage due to multiple instances.
    • Suitable for stateful beans where each instance holds its own state.
  3. Request Scope:

    • Creates a new instance of the bean for every HTTP request.
    • The bean instance is available only for the duration of a single HTTP request-response cycle.
    • Primarily used in web applications.
  4. Session Scope:

    • Creates a single bean instance for a user session.
    • The bean instance is available as long as the users session exists.
    • Also used in web applications.
  5. Global Session Scope:

    • Similar to session scope, but the bean instance is shared across all sessions in a global session.
    • Mainly used in portlet-based applications.
  6. Application Scope:

    • Creates a single instance of the bean for the entire application context.
    • The bean instance is available throughout the applications lifecycle.
    • Suitable for objects that need to be shared across different parts of the application.
  7. WebSocket Scope:

    • Introduced in Spring 4.2 for use with WebSocket-based applications.
    • Creates a bean instance per WebSocket session.
  8. Custom Scopes:

    • Spring allows you to define your own custom scopes by implementing the org.springframework.beans.factory.config.Scope interface.
    • This enables you to create scopes that match specific use cases or requirements of your application.

The choice of bean scope depends on the requirements of your application. For example:

  • Use singleton for stateless and thread-safe beans, as it reduces memory overhead by reusing instances.
  • Use prototype for stateful beans that should have their own distinct instances.
  • Use request and session scopes in web applications to manage bean instances within the context of HTTP requests and sessions.
  • Use application scope for beans that need to be shared across different parts of the application.

Keep in mind that the choice of bean scope affects how Spring manages the instances, and its important to understand the lifecycle implications when selecting the appropriate scope for your beans.


In Spring Framework, beans can have different scopes that define the lifecycle and visibility of instances. The following are the different bean scopes available in Spring:

  1. Singleton Scope (default):

    • In singleton scope, a single instance of the bean is created for the entire application context.
    • This instance is shared among all the clients (classes that request the bean) within the same context.
    • The bean is created when the application context is initialized, and it remains alive throughout the applications lifecycle.
    • You can explicitly define a bean as singleton using the @Scope("singleton") annotation or by omitting the scope annotation altogether (as its the default).
  2. Prototype Scope:

    • In prototype scope, a new instance of the bean is created every time it is requested.
    • Each client that requests the bean gets a separate instance.
    • This scope is useful when you want a new, fresh instance of the bean each time its used.
    • Define a bean as prototype using the @Scope("prototype") annotation.
  3. Request Scope:

    • This scope is only available in a web-aware Spring application.
    • In request scope, a new instance of the bean is created for each HTTP request.
    • This is useful for beans that need to be specific to a single request, such as request-specific data.
    • Use the @Scope("request") annotation to define a bean with request scope.
  4. Session Scope:

    • Like request scope, session scope is also only available in a web-aware Spring application.
    • In session scope, a new instance of the bean is created for each user session.
    • This is useful for beans that need to be specific to a users session.
    • Define a bean with session scope using the @Scope("session") annotation.
  5. Global Session Scope:

    • This scope is also specific to web applications.
    • Global session scope is similar to session scope, but the bean is scoped to the global session, which is usually across multiple HTTP requests.
    • You can define a bean with global session scope using the @Scope("globalSession") annotation.
  6. Application Scope:

    • This scope is available in a web-aware Spring application.
    • In application scope, a single instance of the bean is created for the entire web application context.
    • The bean is created when the application context is initialized and remains alive throughout the applications lifecycle.
    • Use the @Scope("application") annotation to define a bean with application scope.

To specify the scope of a bean, you can use annotations such as @Scope("prototype"), @Scope("singleton"), @Scope("request"), @Scope("session"), @Scope("globalSession"), or @Scope("application"), depending on the scope you want for your bean. The default scope is singleton if no scope is explicitly specified.


here are examples of different Spring bean scopes:

  1. Singleton Scope (Default):
    • In this scope, a single instance of the bean is created and shared across the entire application context.
java
@Component public class SingletonBean { // ... }
  1. Prototype Scope:
    • In this scope, a new instance of the bean is created every time it is requested.
java
@Component @Scope("prototype") public class PrototypeBean { // ... }
  1. Request Scope (Web Application Only):
    • In this scope, a new instance of the bean is created for each HTTP request.
java
@Component @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) public class RequestScopedBean { // ... }
  1. Session Scope (Web Application Only):
    • In this scope, a new instance of the bean is created for each user session.
java
@Component @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) public class SessionScopedBean { // ... }
  1. Global Session Scope (Web Application Only):
    • In this scope, a new instance of the bean is created for each global session (typically spanning multiple HTTP requests).
java
@Component @Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) public class GlobalSessionScopedBean { // ... }
  1. Application Scope (Web Application Only):
    • In this scope, a single instance of the bean is created and shared across the entire web application context.
java
@Component @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS) public class ApplicationScopedBean { // ... }

Please note that the examples with SCOPE_REQUEST, SCOPE_SESSION, SCOPE_GLOBAL_SESSION, and SCOPE_APPLICATION require a web-aware Spring application context. Also, when using request, session, global session, or application scopes, you need to use ScopedProxyMode.TARGET_CLASS to enable proper proxying of the scoped bean, as these scopes are tied to HTTP requests or sessions, and the actual bean creation happens at runtime.

These examples demonstrate how to define beans with different scopes in a Spring application, both for general application contexts and for web application contexts.

Search
Related Articles

Leave a Comment: