How do you define a singleton bean in Spring?
Table of Contents
Introduction
In Spring Framework, beans are objects that are managed by the Spring IoC (Inversion of Control) container. By default, Spring beans are singleton-scoped, meaning the container creates only one instance of the bean, which is shared across the entire application. Defining a singleton bean is one of the most common patterns in Spring-based applications, as it ensures that a single instance is used throughout the lifecycle of the application. This guide explores the different ways to define a singleton bean in Spring.
Methods to Define a Singleton Bean in Spring
1. Using @Component
Annotation
The @Component
annotation is used to define a Spring bean in the context of component scanning. By default, Spring beans defined with @Component
are singleton-scoped.
Example:
In this example, MySingletonBean
is a singleton bean because it is annotated with @Component
, and Spring will automatically create and manage a single instance of this bean.
2. Using @Bean
Annotation in Java Config
If you are using Java-based configuration, you can define a singleton bean by using the @Bean
annotation inside a @Configuration
class. Beans defined with @Bean
are also singleton by default unless you specify a different scope.
Example:
Here, mySingletonBean()
defines a singleton bean of type MySingletonBean
. The Spring container will only create one instance of MySingletonBean
, and it will be shared across the application.
3. Using @Scope("singleton")
Although Spring beans are singleton by default, you can explicitly define a bean’s scope as singleton
using the @Scope
annotation. This can be useful if you want to be explicit about the scope in your configuration.
Example:
In this example, the @Scope("singleton")
annotation ensures that the mySingletonBean()
bean is treated as a singleton. Even though Spring defaults to singleton scope, this annotation provides clarity in your configuration.
Practical Examples
Example 1: Using @Component
to Define Singleton Beans
In this example, DatabaseService
is a Spring-managed component, and it depends on MySingletonBean
, which is also a singleton. Since both beans are singletons by default, Spring injects the same instance of MySingletonBean
into DatabaseService
.
Example 2: Singleton Bean Usage in an Application
In this example, both singletonBean1
and singletonBean2
will refer to the same instance, as they are fetched from the Spring context, which enforces the singleton scope by default.
Conclusion
In Spring, defining a singleton bean is simple, as it is the default scope for beans. You can use annotations like @Component
for automatic component scanning, @Bean
in Java configuration, or explicitly use the @Scope("singleton")
annotation. Regardless of the approach, Spring ensures that only one instance of the bean is created and shared throughout the application, making it ideal for shared resources or stateful services. Understanding how to manage singleton beans is essential for designing efficient and manageable Spring applications.