Contents

The singleton scope is the default. Spring creates exactly one instance per ApplicationContext and every request for that bean returns the same shared instance. Ideal for stateless beans.

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

With prototype scope Spring creates a new instance each time the bean is requested. Use this for stateful beans. Note: Spring does not call destroy callbacks on prototype beans.

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

The request scope creates a new bean for each HTTP request. The bean lives for the duration of the request and is discarded afterwards. Only available in a web-aware ApplicationContext.

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; } }

The session scope creates a new bean per HTTP session. The same instance is shared across all requests within the same user session. Useful for per-user data such as authentication details.

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 if the HTTP session is persisted or replicated across nodes.