Spring - Bean Scopes
Spring allows you to control how many instances of a bean are created and for how long they live. The scope of a bean defines its lifecycle and visibility within the ApplicationContext. The six built-in scopes are: singleton, prototype, request, session, application and websocket.
Singleton scope (default)
The singleton
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
// Singleton is the default — no @Scope annotation needed.
@Service
public class CacheService {
private final Map<String, Object> cache = new ConcurrentHashMap<>();
public void put(String key, Object value) { cache.put(key, value); }
public Object get(String key) { return cache.get(key); }
}
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
CacheService a = ctx.getBean(CacheService.class);
CacheService b = ctx.getBean(CacheService.class);
System.out.println(a == b); // true — same instance
Prototype scope
With prototype
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Scope("prototype")
public class ShoppingCart {
private final List<CartItem> items = new ArrayList<>();
public void addItem(CartItem item) { items.add(item); }
public List<CartItem> getItems() { return items; }
}
ShoppingCart cart1 = ctx.getBean(ShoppingCart.class);
ShoppingCart cart2 = ctx.getBean(ShoppingCart.class);
System.out.println(cart1 == cart2); // false — different instances
Request scope (web-aware)
The request
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
@Component
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class RequestContext {
private String traceId;
public String getTraceId() { return traceId; }
public void setTraceId(String traceId) { this.traceId = traceId; }
}
Session scope (web-aware)
The session
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import java.io.Serializable;
@Component
@Scope(WebApplicationContext.SCOPE_SESSION)
public class UserSessionData implements Serializable {
private String loggedInUser;
private String preferredLanguage = "en";
public String getLoggedInUser() { return loggedInUser; }
public void setLoggedInUser(String user) { this.loggedInUser = user; }
public String getPreferredLanguage() { return preferredLanguage; }
public void setPreferredLanguage(String lang) { this.preferredLanguage = lang; }
}
Note: Session-scoped beans must be Serializable