Backend Development 5 min read

Understanding Spring Bean Scopes: Singleton vs Prototype

This article explains the differences between Spring's singleton and prototype bean scopes, describing how singleton beans are cached and shared across requests while prototype beans are created anew each time, and discusses their performance advantages, thread‑safety drawbacks, and practical implications for developers.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Understanding Spring Bean Scopes: Singleton vs Prototype

Hello, I am the "Architecture Jun", a developer who also writes poetry.

Spring provides five bean scopes—singleton, prototype, request, session, and global session—but this article focuses on the first two, which were the original scopes offered by Spring.

When a bean is defined as a singleton, Spring creates only one instance and stores it in a map; every subsequent request retrieves the same instance from the cache, avoiding repeated instantiation.

In contrast, a prototype bean is instantiated anew for each request, with no caching involved.

Diagram analysis shows the lifecycle of singleton versus prototype beans, illustrating that singletons are created once and then reused, while prototypes are created on demand.

Source‑code analysis reveals that during bean creation Spring first checks the scope: if it is singleton, it attempts to fetch the bean from the cache and creates it only if absent; if it is prototype, it directly creates a new instance.

Advantages of singleton beans include reduced instance‑creation overhead, lower JVM garbage‑collection pressure, and faster bean retrieval because most accesses hit the cache.

The main disadvantage is that a singleton bean is not thread‑safe when it holds mutable state, which can cause concurrency issues in high‑traffic scenarios; prototype beans do not share this problem.

In summary, Spring defaults to singleton beans to improve performance by minimizing object creation, reducing garbage collection, and enabling quick cache retrieval, while developers must be aware of potential thread‑safety concerns for stateful singletons.

BackendJavaSpringsingletonPrototypeBean Scope
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.