Singleton Design Pattern in Java and Spring Boot: Understand and See it in Practice!

Singleton Design Pattern in Java and Spring Boot: Understand and See it in Practice!

In the world of object-oriented programming, Design Patterns are reusable solutions to recurring problems in software development. One of the most well-known and widely used patterns is the Singleton. In this post, I'll explain what the Singleton is, when to use it, and provide a practical example in Java and how to apply it in Spring Boot.

What is the Singleton Design Pattern?

The Singleton is a design pattern that ensures a class has only one instance throughout the application's lifecycle and provides a global access point to that instance.

This pattern is useful in situations where centralized control is required, such as:

  • Configuration managers;
  • Database connections;
  • Loggers;
  • Cache controllers.

When to Use the Singleton?

The Singleton should be used when you need to ensure that only one instance of a class is created and that this instance is shared across different parts of the application.

⚠️ Attention: While the Singleton is very useful, it should be used sparingly. If misused, it can introduce unwanted dependencies and make unit testing difficult.

Practical Example of Singleton in Java

Let's create a simple example of a Logger using the Singleton pattern.

Article content

How to Use the Singleton:

Article content

How Does the Code Work?

  1. The getInstance() method checks if the instance has already been created. If not, it creates a new instance.
  2. The Logger class constructor is private, preventing other classes from directly instantiating this class.
  3. Once the instance is created, all subsequent calls to getInstance() return the same instance.

Benefits of the Singleton

  • Centralized control: Easy control of shared resources.
  • Efficiency: Saves resources by avoiding the creation of multiple instances.
  • Consistency: Ensures that all parts of the application use the same instance.

Cautions When Using Singleton

  • It can introduce tight coupling in the application.
  • Difficult to test with unit tests.
  • Can lead to concurrency issues in multi-threaded applications.

➡️ To avoid concurrency issues, consider using the thread-safe Singleton approach.

Article content

Applying the Singleton in Spring Boot

In Spring Boot, the Singleton pattern is automatically implemented for Beans registered in the application context. By default, Beans in Spring Boot have a singleton scope, meaning that only one instance of the Bean is created and managed by the Spring container.

Example with Spring Boot:

Article content

In this example, the LoggerService class is annotated with @Service, which causes Spring to register it as a Singleton Bean. There's no need to manually implement the Singleton pattern because Spring ensures that only one instance of LoggerService will be created.

How to Use the Singleton Bean in a Class:

Article content

In this example, the LoggerController class injects the LoggerService using the constructor. Spring ensures that the same instance of LoggerService is used throughout the application.

I hope this post has helped you understand the Singleton Design Pattern, how to apply it in pure Java, and how Spring Boot already facilitates the implementation of this pattern! 🚀

Did you like the content? Comment and share your experiences with Design Patterns!

To view or add a comment, sign in

Others also viewed

Explore content categories