All articles
Spring CoreSpring Boot

Spring Bean Scopes Explained: Singleton, Prototype, and Web Scopes

March 3, 20263 min read

Overview of Spring Bean Scopes

Spring supports 6 bean scopes. The scope determines how many instances Spring creates and for how long they live.

ScopeInstancesAvailable In
singletonOne per ApplicationContextAll contexts
prototypeNew instance per requestAll contexts
requestOne per HTTP requestWeb contexts only
sessionOne per HTTP sessionWeb contexts only
applicationOne per ServletContextWeb contexts only
websocketOne per WebSocket sessionWeb contexts only

Singleton (Default)

One shared instance per ApplicationContext. All calls to getBean() return the same object.

@Component
// @Scope("singleton") — this is the default, no annotation needed
public class OrderService {
    // One instance shared across the entire application
}

Use for: stateless services, repositories, configuration beans.

Exam tip: Singleton scope is per-ApplicationContext, not per-JVM. Two separate contexts in the same JVM create two different singleton instances.


Prototype

A new instance is created every time the bean is requested.

@Component
@Scope("prototype")
public class ShoppingCart {
    private List<Item> items = new ArrayList<>();
    // Each request gets a fresh cart
}

Use for: stateful beans that should not be shared.

Injecting Prototype into Singleton

This is a classic exam trap. If you inject a prototype bean into a singleton, the prototype is only created once (at singleton initialization) — defeating its purpose.

@Component
public class SingletonService {

    @Autowired
    private PrototypeBean bean; // Always the same instance! Bug!
}

Solutions:

  • Use ApplicationContext.getBean() programmatically
  • Use @Lookup method injection
  • Use ObjectProvider<PrototypeBean>

Request Scope

One bean instance per HTTP request. The bean is created when the request arrives and destroyed when it ends.

@Component
@RequestScope // shorthand for @Scope("request")
public class RequestContext {
    private String correlationId;
}

Session Scope

One bean instance per HTTP session. Persists across multiple requests from the same user.

@Component
@SessionScope
public class UserPreferences {
    private String theme = "dark";
}

Application Scope

One instance per ServletContext — similar to singleton but scoped to the web application context.

@Component
@ApplicationScope
public class AppConfig {
    // Shared across all users and requests
}

Exam Quick Reference

Q: What is the default scope? singleton

Q: Which scopes require a web-aware ApplicationContext? request, session, application, websocket

Q: A prototype bean injected into a singleton bean — how many instances are created? Just one, at the time the singleton is initialized. Use @Lookup or ObjectProvider to get a new prototype each time.

Q: Does Spring call @PreDestroy on prototype beans? No. Spring does not manage the full lifecycle of prototype beans.

Practice This Topic

Reinforce what you've learned with free practice questions and detailed explanations.