Singleton Bean

About

In Spring, a singleton bean is a bean that is instantiated only once per Spring container and shared across the entire application. By default, all beans in Spring are singleton-scoped, meaning Spring creates a single instance of the bean and returns the same instance whenever it is requested.

How Singleton Beans Work in Spring?

When a singleton bean is defined, Spring:

  1. Creates the bean instance once during container startup.

  2. Stores it in a cache (BeanFactory registry).

  3. Returns the same instance whenever the bean is requested.

  4. Manages the lifecycle (initialization, destruction, etc.).

Example: Singleton Bean in Spring

@Component
public class UserService {
    public void serve() {
        System.out.println("Serving user...");
    }
}

Now, if multiple classes request this bean:

userService1 and userService2 will point to the same instance of UserService.

Declaring Singleton Beans

By default, Spring beans are singleton-scoped. You can explicitly declare a singleton bean using:

Using @Component (default singleton)

Using @Bean in Java Configuration

Using XML Configuration (Legacy)

Singleton Scope vs JVM-Level Singleton

A singleton in Java (using static or enum) ensures only one instance per JVM, while a Spring singleton ensures one instance per Spring container.

Comparison

Feature
Spring Singleton
Java Singleton (Static/Enum)

Instance Count

One per Spring container

One per JVM

Lazy Loading

Yes (if configured)

No

Dependency Injection

Yes

No

Thread Safety

Managed by Spring

Needs manual handling

Flexibility

Configurable

Hardcoded

Example of a JVM singleton:

Singleton Beans in Multi-Threaded Environments

Singleton beans are shared across all threads, potentially causing race conditions.

Thread Safety Concerns

Here, count may be inconsistent if multiple threads modify it simultaneously.

Thread-Safe Singleton

  • Use synchronized blocks

  • Use ThreadLocal variables

  • Use ConcurrentHashMap for shared state

Example:

Singleton Beans in Microservices (Spring Boot)

In Spring Boot, singleton beans are often used in:

  • Service Layer (@Service) – Business logic layer

  • Repository Layer (@Repository) – Database interactions

  • Controller Layer (@RestController) – Handling HTTP requests

Example: Singleton in Microservices

Here, OrderService is singleton, ensuring a single instance handles all requests.

Last updated