A singleton instance is shared by concurrent callers; keep request-specific values outside its fields and publish shared state safely.
Spring singleton state: one bean does not mean one request at a time
Scope is an identity rule
Spring's default singleton scope creates one bean instance per container definition. It does not serialize calls to that instance. A receipt pricer used by many HTTP requests must not keep the current tenant or a mutable calculation buffer in instance fields, because another thread can overwrite them mid-request. Constructor-injected collaborators can remain shared when they are safe for concurrent use. Bean scope controls instance creation; it is not a locking strategy.
Make the state contract explicit
The example copies a startup rate table into an immutable map and keeps currency and amount as method arguments. If rates must change while the process runs, publish an immutable snapshot through an AtomicReference or use a store with an explicit update protocol. A ConcurrentHashMap prevents structural corruption but does not make a read-modify-write business operation atomic. Per-request information can live in a request-scoped object when an HTTP request truly owns its lifetime; request proxies have their own boundary.
Test interleaving, not just output
Launch two requests for different tenants with a barrier between rate lookup and calculation. Verify neither can observe the other's tenant or amount. Run the test repeatedly under a thread pool; a single sequential test cannot expose a field race. For a hot rate reload, test that each calculation reads one complete snapshot rather than a mixture of old and new entries. Async work may outlive the HTTP request, so pass explicit values rather than reaching into request state later.
Implementation contract
@Service
final class ReceiptPricing {
private final Map<String, BigDecimal> rates;
ReceiptPricing(ReceiptRateProperties settings) {
this.rates = Map.copyOf(settings.rates());
}
BigDecimal total(String currency, BigDecimal subtotal) {
BigDecimal rate = Objects.requireNonNull(rates.get(currency));
return subtotal.multiply(rate).setScale(2, RoundingMode.HALF_UP);
}
}Cost and verification
An immutable snapshot adds one copy at startup and allows lock-free reads afterward. A mutable shared table may save a copy but requires a coherent update protocol and contention control under concurrent requests.
Common Mistakes
- Do not store the current tenant, request ID or intermediate total in singleton fields.
- Do not assume ConcurrentHashMap makes a multi-step business update atomic.
- Do not inject a prototype once into a singleton and expect a new collaborator for every call.
Read next
Spring bean scopes: singleton identity is not thread safety, Spring request scope proxy: resolve state for the current HTTP request, Spring constructor injection: required dependencies stay visible, Spring task executors: capacity, rejection and lost context, Spring @Lookup: obtain a fresh prototype from a singleton method.
