Bean Scopes in Spring


Spring offers several bean scopes, each with its advantages and disadvantages. This post will delve into the various bean scopes, explore their pros and cons, and discuss which scope developers should prefer and why.

What is Bean Scope?

In Spring, a bean is an object instantiated, assembled, and managed by the Spring IoC container. Bean scope determines the lifecycle and visibility of these beans within the Spring container. Spring offers several bean scopes, including singleton, prototype, request, session, and application scopes.

Bean Scopes in Spring

Similar Post: Spring Bean Life Cycle Management Example

1. Singleton Scope

Pros:

  1. Default scope in Spring.
  2. One instance per Spring IoC container.
  3. Suitable for stateless beans.
  4. Promotes performance as only one instance is created.

Cons:

  1. Not suitable for stateful beans where each client needs its own instance.
  2. Can lead to thread safety issues if not handled properly.

Code Snippet:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("singleton")
public class SingletonBean {
    // Class implementation
}

2. Prototype Scope

Pros:

  1. A new instance is created each time the bean is requested.
  2. Suitable for stateful beans where each client needs its own instance.
  3. No synchronization issues as instances are independent.

Cons:

  1. Can lead to performance overhead due to frequent object creation.
  2. Requires proper resource management to avoid memory leaks.

Code Snippet:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class PrototypeBean {
    // Class implementation
}

3. Request Scope

Pros:

  1. One instance per HTTP request.
  2. Suitable for web applications to maintain state during a single request.
  3. Prevents thread safety issues in a multi-threaded environment.

Cons:

  1. Limited to web applications.
  2. Increased memory consumption for large-scale applications with numerous concurrent requests.

Code Snippet:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.RequestScope;

@Component
@RequestScope
public class RequestScopedBean {
    // Class implementation
}

4. Session Scope

Pros:

  1. One instance per HTTP session.
  2. Maintains state across multiple requests from the same client.
  3. Useful for storing user-specific data in web applications.

Cons:

  1. Consumes memory as long as the session is active.
  2. Not suitable for applications with heavy session usage or limited memory resources.

Code Snippet:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.SessionScope;

@Component
@SessionScope
public class SessionScopedBean {
    // Class implementation
}

5. Application Scope

Pros:

  1. One instance per ServletContext.
  2. Useful for storing global objects accessible throughout the application.
  3. Provides a centralized location for shared resources.

Cons:

  1. Limited to web applications.
  2. Potential for memory leaks if objects are not properly managed.

Code Snippet:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.ApplicationScope;

@Component
@ApplicationScope
public class ApplicationScopedBean {
    // Class implementation
}

Which Scope to Prefer?

The choice of bean scope depends on the specific requirements and characteristics of the application:

  1. Singleton: Use when a single instance of the bean is sufficient for the entire application and when performance optimization is crucial.
  2. Prototype: Suitable for stateful beans where each client needs its own instance or when the bean’s state needs to be reset on each request.
  3. Request and Session: Ideal for web applications to maintain state across HTTP requests. Choose request scope for short-lived state and session scope for longer-lived state.
  4. Application: Use when global objects or resources need to be shared across the entire application.

Summary

Understanding Spring bean scope is essential for designing scalable, maintainable, and efficient applications. Each scope has its advantages and limitations, and the choice depends on the specific requirements of the application. By selecting the appropriate scope, you can optimize resource utilization and ensure the desired behavior of their Spring beans.

References

  1. Bean Scopes- Spring’s Docx

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.