What is the significance of the @Scope annotation?
Table of Contents
- Introduction
- Conclusion
Introduction
In Spring, bean scope defines the lifecycle and visibility of a bean within the Spring ApplicationContext. The **@Scope**
annotation plays a crucial role in managing how beans are created and how they exist in the Spring container. By default, beans in Spring are singleton-scoped, meaning that only one instance of a bean is created and shared across the application. However, there are many cases where you might need a bean to be created with a different lifecycle, such as creating a new instance every time it is requested or limiting its lifespan to the duration of an HTTP request.
The @Scope
annotation allows developers to customize the scope of beans and manage their lifecycle more effectively. In this guide, we will explore the different types of bean scopes in Spring and the significance of using the @Scope
annotation.
1. What is the @Scope
Annotation?
The @Scope
annotation in Spring allows you to specify the scope of a bean, which determines how many instances of that bean will exist within the application context and how those instances will be created and destroyed.
Spring provides several predefined scopes for beans:
- Singleton: One instance per Spring container (default scope).
- Prototype: A new instance each time the bean is requested.
- Request: One instance per HTTP request (only available in web applications).
- Session: One instance per HTTP session (only available in web applications).
- Application: One instance per ServletContext (only available in web applications).
- WebSocket: One instance per WebSocket session (only available in web applications).
2. Basic Usage of the @Scope
Annotation
The @Scope
annotation can be used on beans to define the lifecycle behavior of that bean. It can be applied alongside annotations like @Component
, @Service
, @Repository
, or @Configuration
to indicate the scope of a particular bean.
Example: Defining a Singleton-Scoped Bean
By default, Spring beans are singleton-scoped, so the @Scope
annotation is optional for singleton beans. However, for clarity, you can explicitly specify the singleton scope:
In this example:
- The
SingletonBean
is scoped as a singleton, meaning only one instance will be created and shared by all clients within the Spring context.
Example: Defining a Prototype-Scoped Bean
In this example:
- The
PrototypeBean
is scoped as prototype, meaning that a new instance ofPrototypeBean
will be created each time it is requested from the Spring container.
3. Available Scopes in Spring
Spring provides several built-in scopes that you can use with the @Scope
annotation. Let’s look at each of them in more detail:
1. Singleton Scope (Default)
The default scope for Spring beans is singleton, which means only one instance of the bean is created for the entire application context. All requests for that bean will return the same instance.
- Scope name:
"singleton"
- Instances: 1 per Spring container
- Use case: Beans that are stateless or share data across the entire application.
2. Prototype Scope
In prototype scope, Spring will create a new bean instance every time it is requested from the application context. This is useful when you want each use of the bean to be independent and have its own state.
- Scope name:
"prototype"
- Instances: 1 per request to the Spring container
- Use case: Beans that are stateful or need a fresh instance each time.
3. Request Scope (Web Applications)
In request scope, a new instance of the bean is created for each HTTP request. This is only applicable in web applications.
- Scope name:
"request"
- Instances: 1 per HTTP request
- Use case: Beans that should only be active for the duration of a single HTTP request.
4. Session Scope (Web Applications)
In session scope, a new instance of the bean is created for each HTTP session. This is useful when you want a bean to persist throughout a user's session.
- Scope name:
"session"
- Instances: 1 per HTTP session
- Use case: Beans that are specific to a user session, such as user authentication objects.
5. Application Scope (Web Applications)
In application scope, a single bean is created per ServletContext. This means the bean will be shared across all users and requests within the entire web application.
- Scope name:
"application"
- Instances: 1 per application context (ServletContext)
- Use case: Beans that should be shared across the entire application, such as application-wide caches or configuration services.
6. WebSocket Scope (Web Applications)
In WebSocket scope, a new bean is created for each WebSocket session. This scope is useful in real-time, bidirectional communication applications, such as chat apps or live updates.
- Scope name:
"websocket"
- Instances: 1 per WebSocket session
- Use case: Beans for real-time communication, such as WebSocket connections.
4. Custom Scopes
Spring also allows you to define custom bean scopes. You can create a custom scope by implementing the org.springframework.beans.factory.config.Scope
interface and registering it with the Spring container. This is useful when you need very specific lifecycle behavior.
5. Practical Example: Using @Scope
with @Autowired
When you use @Scope
on a bean, Spring will create a new instance each time it is injected via @Autowired
. Below is an example of a singleton bean (SingletonBean
) that autowires a prototype-scoped bean (PrototypeBean
).
Example: Singleton Bean Injecting a Prototype Bean
In this example:
SingletonBean
is a singleton bean.PrototypeBean
is a prototype bean.- Each time
SingletonBean
callsdisplayPrototype()
, a new instance ofPrototypeBean
is created.
Conclusion
The @Scope
annotation in Spring provides a powerful way to manage the lifecycle and visibility of beans within the application context. By specifying different scopes such as singleton, prototype, request, session, and application, you can fine-tune how Spring manages the creation and destruction of beans in your application. Understanding and using bean scopes appropriately can significantly improve the flexibility and efficiency of your Spring applications.
Whether you're working on stateless services, stateful services, or handling user sessions, the @Scope
annotation gives you the control you need over bean lifecycles and helps you build scalable, maintainable applications.