What are Singleton Beans in Spring?
Singleton Bean is the default bean scope in Spring Framework where only one instance of a bean is created for the entire Spring IoC container.
This single bean object is shared across the entire application whenever required.
In simple terms:
- Only one object is created
- The same object is reused everywhere
- Spring manages the object centrally
- All components share the same bean instance
Singleton Beans are heavily used in:
- Spring Framework
- Spring Boot
- Microservices Architecture
- Enterprise Applications
- Cloud-Native Systems
Simple Definition
A Singleton Bean means Spring creates only one instance of the bean per Spring container.
Real-Time Banking Example
Consider a banking application:
- Payment Service
- Notification Service
- Fraud Detection Service
If PaymentService is configured as Singleton:
- Only one PaymentService object is created
- All requests use the same object
- Memory usage becomes efficient
How Singleton Beans Work Internally
Application Starts
|
Spring IoC Container Initializes
|
Creates One Bean Instance
|
Stores Bean in Container Cache
|
Same Bean Shared Everywhere
Singleton Bean Example
@Service
public class PaymentService {
}
By default:
- Spring creates only one PaymentService object
- The same object is reused throughout the application
Default Scope in Spring
Singleton is the default scope in Spring.
Even without specifying scope:
@Service
public class PaymentService {
}
Spring automatically treats it as:
@Scope("singleton")
Explicit Singleton Scope Example
@Service
@Scope("singleton")
public class PaymentService {
}
How Singleton Bean is Shared
Controller A
|
|----> Same PaymentService Object
|
Controller B
|
|----> Same PaymentService Object
|
Controller C
All components share the same bean instance.
Verifying Singleton Scope
Example
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
PaymentService service1 =
context.getBean(PaymentService.class);
PaymentService service2 =
context.getBean(PaymentService.class);
System.out.println(service1 == service2);
Output
true
Both references point to the same object.
Why Singleton Beans are Important
Enterprise applications may contain:
- Thousands of requests
- Large numbers of services
- Complex object graphs
Creating multiple objects repeatedly can:
- Increase memory usage
- Reduce performance
- Increase garbage collection
Singleton scope solves these problems efficiently.
Advantages of Singleton Beans
- Memory efficient
- Better performance
- Reduced object creation
- Faster application execution
- Centralized object management
1. Memory Efficiency
Only one object exists for the entire application.
This reduces memory consumption significantly.
2. Better Performance
Object creation is expensive.
Singleton beans avoid repeated object creation.
3. Faster Application Execution
Reusing the same object improves execution speed.
4. Centralized Management
Spring manages a single shared object centrally.
Singleton Bean Lifecycle
Application Starts
|
Singleton Bean Created
|
Bean Initialized
|
Bean Shared Across Application
|
Application Shutdown
|
Bean Destroyed
Singleton Beans and Thread Safety
Singleton beans are shared across multiple threads.
Therefore:
- Singleton beans should ideally be stateless
- Avoid mutable shared variables
- Thread safety becomes important
Unsafe Singleton Example
@Service
public class CounterService {
private int count = 0;
public void increment() {
count++;
}
}
Multiple threads may update count simultaneously causing issues.
Safe Singleton Example
@Service
public class PaymentService {
public void processPayment() {
System.out.println("Processing");
}
}
Stateless singleton beans are safe.
Singleton Bean vs Prototype Bean
| Feature | Singleton Bean | Prototype Bean |
|---|---|---|
| Object Creation | One Instance | New Instance Every Request |
| Memory Usage | Low | High |
| Performance | Better | Slower |
| Default Scope | Yes | No |
When to Use Singleton Beans
- Business services
- Configuration services
- Database services
- REST controllers
- Utility services
When Not to Use Singleton Beans
- User-specific state management
- Temporary session data
- Request-specific objects
Singleton Beans in Spring Boot
Spring Boot heavily uses singleton beans for:
- REST APIs
- Database repositories
- Security configuration
- Microservices communication
- Cloud-native applications
Singleton Beans in Microservices
In Microservices Architecture:
- Services are usually singleton beans
- Controllers are singleton beans
- Repositories are singleton beans
This improves:
- Performance
- Scalability
- Memory efficiency
Microservices Banking Example
PaymentService Bean
|
NotificationService Bean
|
FraudDetectionService Bean
Each service bean exists as a single shared instance.
Common Spring Annotations Related to Singleton Beans
- @Component
- @Service
- @Repository
- @Controller
- @Bean
- @Scope("singleton")
Real-World Enterprise Use Cases
- Banking systems
- E-commerce platforms
- Healthcare systems
- Cloud-native applications
- Distributed microservices systems
Common Interview Questions
What is Singleton Bean in Spring?
Singleton Bean is the default Spring bean scope where only one bean instance is created for the entire Spring container.
Why are Singleton Beans used?
Singleton Beans improve performance, reduce memory usage, and avoid repeated object creation.
Are Singleton Beans thread-safe?
Singleton beans are shared across threads, so they should ideally be stateless for thread safety.
Professional Interview Answer
Singleton Bean is the default bean scope in Spring Framework where only one instance of a bean is created and shared throughout the entire Spring IoC container. The same bean object is reused whenever required, improving memory efficiency and application performance. Singleton beans are commonly used for services, repositories, controllers, and configuration classes in enterprise applications and microservices architectures.
Conclusion
Singleton Beans are one of the most important concepts in Spring Framework and Spring Boot.
They allow Spring to efficiently manage shared bean instances across enterprise applications, reducing memory consumption and improving application performance.
Singleton scope is widely used in modern banking systems, e-commerce applications, microservices architectures, and cloud-native platforms because it provides scalability, centralized management, and efficient resource utilization.
Understanding Singleton Beans is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.