How do you create a prototype-scoped bean in Spring?

Table of Contents

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:

  1. Bean creation: A new instance is created whenever the bean is requested.
  2. Initialization: Spring will apply initialization logic (e.g., @PostConstruct or custom init-method).
  3. 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 the MyService bean should be prototype-scoped.
  • **@Bean**: Registers MyService 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 registers MyService as a Spring bean.
  • The scope="prototype" attribute tells Spring to create a new instance of MyService 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 and counter2 are separate instances of CounterService 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

FeatureSingleton ScopePrototype Scope
Bean CreationOne instance per Spring containerNew instance every time it is requested
ScopeShared across the entire containerIndependent instances each time requested
Lifecycle ManagementSpring manages lifecycle (destroy, init)Spring does not manage lifecycle after init
UsageSuitable for stateless beansSuitable 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.

Similar Questions