LeetCampus
Interview Question

How we can set the spring bean scope. And what supported scopes does it have?

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Spring Bean Scope refers to the lifecycle and visibility of a bean in the Spring Framework. Understanding bean scopes is crucial for effective resource management and application performance. Interviewers ask this question to assess a candidate's knowledge of dependency injection and the Spring container's behavior. The most common scopes are:

  • Singleton: One instance per Spring container.
  • Prototype: A new instance every time a bean is requested.
  • Request: A new instance for each HTTP request (web applications).
  • Session: A new instance for each HTTP session (web applications).
  • Global Session: A new instance for each global HTTP session (portlet applications). Knowing how to set these scopes correctly impacts resource utilization and application stability. Misunderstandings can lead to memory leaks or performance bottlenecks, particularly in larger applications. This question tests a candidate's practical experience and their understanding of when to use each scope effectively.

Sample Answers

Example 1: Understanding Singleton Scope

In Spring, the Singleton scope means that the Spring container creates a single instance of the bean and shares it across the application context. To set a bean's scope as Singleton, you can use the @Scope annotation like this:

@Bean
@Scope("singleton")
public MyBean myBean() {
    return new MyBean();
}

This is the default scope, so if you omit the @Scope annotation, the bean will still be a singleton. It's ideal for stateless beans where a single instance can handle multiple requests, thus saving memory and improving performance. However, be cautious with mutable state because shared instances can lead to unexpected behavior if not managed properly.

Example 2: Using Prototype Scope

The Prototype scope creates a new instance of the bean every time it is requested from the Spring container. This can be set using the @Scope annotation:

@Bean
@Scope("prototype")
public MyBean myBean() {
    return new MyBean();
}

Prototype beans are especially useful when you need to maintain a distinct state for each instance, such as when handling user-specific data. However, developers should be aware that Spring does not manage the lifecycle of prototype beans after creation, meaning it's the developer's responsibility to handle resource cleanup to avoid memory leaks.

Example 3: Request and Session Scopes in Web Apps

In web applications, Request and Session scopes are vital for managing user interactions. The Request scope means a new bean instance is created for each HTTP request:

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public MyBean myBean() {
    return new MyBean();
}

Conversely, the Session scope allows a bean to exist for the duration of an HTTP session:

@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION)
public MyBean myBean() {
    return new MyBean();
}

Utilizing these scopes effectively ensures that user data is encapsulated correctly during their session, improving the application's responsiveness and user experience.

Keywords

Spring FrameworkBean ScopeDependency InjectionJavaSoftware Engineering

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions