How do you create a prototype-scoped bean in Spring?
Table of Contents
- Introduction
- Understanding Prototype Scope
- Creating a Prototype-Scoped Bean in Spring
- Practical Example of Prototype Scope
- Key Differences Between Singleton and Prototype Scopes
- Conclusion
Introduction
In Spring, the default scope for beans is singleton, which means that only one instance of a bean is created per Spring container, and that instance is shared throughout the application. However, there are situations where you may need a new, independent instance of a bean every time it is requested. This is where the prototype scope comes into play.
A prototype-scoped bean means that Spring will create a new instance of the bean each time it is requested from the ApplicationContext. This scope is useful when you need beans to maintain their state independently and you want a fresh instance for each usage.
In this guide, we'll walk through how to create a prototype-scoped bean in Spring using both Java-based configuration and XML configuration.
Understanding Prototype Scope
When a Spring bean is defined as prototype-scoped, Spring will:
- Create a new instance of the bean every time it is requested.
- Not manage the lifecycle of the prototype bean after creation (i.e., Spring will not call destroy methods on it automatically).
- Not cache the bean in the container like it does with singleton beans.
Bean Lifecycle with Prototype Scope
The lifecycle of a prototype-scoped bean differs from that of a singleton bean:
- Bean creation: A new instance is created whenever the bean is requested.
- Initialization: Spring will apply initialization logic (e.g.,
@PostConstruct
or custominit-method
). - Destruction: The prototype bean is not managed by the Spring container beyond initialization, meaning Spring does not call the destroy method on prototype beans automatically. You must manually handle the destruction if needed.
Creating a Prototype-Scoped Bean in Spring
1. Java-Based Configuration (Using @Scope
Annotation)
The **@Scope**
annotation is used to define the scope of a bean in Spring. To create a prototype-scoped bean in Java-based configuration, you simply use the @Scope
annotation with the value ConfigurableBeanFactory.SCOPE_PROTOTYPE
.
Example:
In this example:
**@Scope("prototype")**
: Specifies that theMyService
bean should be prototype-scoped.**@Bean**
: RegistersMyService
as a bean in the Spring application context.
Each time myService()
is called, Spring will create a new instance of MyService
.
Using the Prototype Bean:
Output:
Each time getBean(MyService.class)
is called, a new instance is returned.
2. XML-Based Configuration (Using scope
Attribute)
In XML configuration, you can define the scope of a bean by using the scope
attribute in the <bean>
element.
Example:
In this example:
- The
<bean>
element registersMyService
as a Spring bean. - The
scope="prototype"
attribute tells Spring to create a new instance ofMyService
every time it is requested.
Using the Prototype Bean in XML:
And in your main Java code:
Each time getBean(MyService.class)
is called, Spring will provide a new instance of MyService
.
Practical Example of Prototype Scope
Let’s say you have a CounterService
that increments a count each time you call a method. You need a fresh instance of CounterService
for each request to ensure the count is not shared between different requests.
Example:
Configuration:
Main Application:
In this example:
counter1
andcounter2
are separate instances ofCounterService
because the bean is prototype-scoped.- The counts are independent of each other, ensuring that the state of one bean does not affect the other.
Key Differences Between Singleton and Prototype Scopes
Feature | Singleton Scope | Prototype Scope |
---|---|---|
Bean Creation | One instance per Spring container | New instance every time it is requested |
Scope | Shared across the entire container | Independent instances each time requested |
Lifecycle Management | Spring manages lifecycle (destroy, init) | Spring does not manage lifecycle after init |
Usage | Suitable for stateless beans | Suitable for stateful beans or independent instances |
Conclusion
Creating a prototype-scoped bean in Spring ensures that a new instance of a bean is created every time it is requested from the application context. This is useful for scenarios where you need independent instances, such as stateful beans or those requiring custom initialization each time they are accessed.
By using either Java-based configuration with the @Scope
annotation or XML configuration with the scope
attribute, Spring gives you flexibility in defining and managing the lifecycle of prototype-scoped beans.