What are Prototype Beans in Spring?
Prototype Bean is a bean scope in Spring Framework where a new bean instance is created every time the bean is requested from the Spring IoC container.
Unlike Singleton scope, where only one object is shared across the application, Prototype scope creates a completely new object for each request.
In simple terms:
- Every request gets a new object
- Objects are not shared
- Spring creates multiple instances
- Suitable for stateful objects
Prototype Beans are used in:
- Spring Framework
- Spring Boot
- Enterprise Applications
- Stateful Processing Systems
- Microservices Architectures
Simple Definition
A Prototype Bean means Spring creates a new bean object every time the bean is requested.
Real-Time Banking Example
Consider a banking application generating:
- Transaction Reports
- Temporary Audit Sessions
- User-Specific Data Objects
Each request requires separate data and separate object state.
In such cases:
- Shared singleton objects are not suitable
- Separate objects are required
Prototype Beans solve this problem.
How Prototype Beans Work Internally
Application Starts
|
Spring IoC Container Initializes
|
Bean Requested
|
New Bean Object Created
|
Bean Returned
|
Next Request
|
Another New Object Created
Prototype Bean Example
@Service
@Scope("prototype")
public class ReportService {
}
Every time ReportService is requested:
- Spring creates a new object
- Objects are not reused
Verifying Prototype Scope
Example
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
ReportService service1 =
context.getBean(ReportService.class);
ReportService service2 =
context.getBean(ReportService.class);
System.out.println(service1 == service2);
Output
false
Both references point to different objects.
Difference Between Singleton and Prototype Beans
| Feature | Singleton Bean | Prototype Bean |
|---|---|---|
| Object Creation | One Object | New Object Every Request |
| Object Sharing | Shared | Not Shared |
| Memory Usage | Low | High |
| Performance | Better | Slightly Slower |
| Default Scope | Yes | No |
Why Prototype Beans are Important
Some applications require:
- User-specific data
- Temporary processing objects
- Independent object states
- Request-specific processing
Shared singleton objects may cause:
- Data corruption
- Thread safety issues
- Unexpected behavior
Prototype Beans help avoid these problems.
Advantages of Prototype Beans
- Independent object instances
- Better state isolation
- Thread safety support
- Suitable for stateful applications
- Flexible object handling
1. Independent Object Instances
Each request gets a separate bean object.
No object sharing occurs.
2. Better State Isolation
Each object maintains its own state independently.
3. Improved Thread Safety
Since objects are not shared:
- Thread conflicts reduce
- Concurrency becomes safer
4. Suitable for Stateful Objects
Stateful objects store temporary or user-specific data.
Prototype scope is ideal for such cases.
Disadvantages of Prototype Beans
- Higher memory usage
- Increased object creation
- Lower performance compared to singleton
- Spring does not manage full destruction lifecycle
Prototype Bean Lifecycle
Bean Requested
|
New Bean Created
|
Dependency Injection
|
Initialization
|
Bean Returned
|
Spring Stops Managing Bean
Important Point About Prototype Beans
Spring manages:
- Bean creation
- Dependency injection
- Initialization
But Spring does NOT automatically manage:
- Bean destruction
- Cleanup operations
Developers must handle cleanup manually if required.
Prototype Bean with Stateful Example
@Service
@Scope("prototype")
public class UserSession {
private String username;
public void setUsername(String username) {
this.username = username;
}
}
Every user gets a separate session object.
Prototype Bean vs Singleton Bean Example
Singleton
One Shared Object
Prototype
New Object Per Request
Prototype Beans and Dependency Injection
Prototype beans can be injected into singleton beans.
However:
- Special handling may be required
- ObjectProvider or ApplicationContext may be used
Example Using ObjectProvider
@Autowired
private ObjectProvider<ReportService>
reportServiceProvider;
public void process() {
ReportService service =
reportServiceProvider.getObject();
}
When to Use Prototype Beans
- User sessions
- Temporary processing objects
- File upload processing
- Transaction-specific data
- Dynamic report generation
When Not to Use Prototype Beans
- Shared services
- Database repositories
- Configuration classes
- REST controllers
Singleton scope is better for these.
Prototype Beans in Spring Boot
Spring Boot supports prototype beans for:
- Request-specific processing
- Stateful operations
- Temporary business objects
- Dynamic processing components
Prototype Beans in Microservices
In Microservices Architecture:
- Most services are singleton
- Some temporary processing objects use prototype scope
Example:
- Request processors
- Dynamic report generators
- Temporary transaction objects
Microservices Banking Example
Transaction Request
|
New Transaction Processor Object
|
Process Transaction
|
Object Discarded
Common Spring Annotations Related to Prototype Beans
- @Component
- @Service
- @Repository
- @Controller
- @Bean
- @Scope("prototype")
Real-World Enterprise Use Cases
- Banking transaction processors
- Shopping cart systems
- Dynamic report generation
- Session management
- File processing systems
Common Interview Questions
What is Prototype Bean in Spring?
Prototype Bean is a Spring bean scope where a new bean object is created every time the bean is requested.
What is the difference between Singleton and Prototype Beans?
Singleton creates one shared object, while Prototype creates a new object for every request.
Does Spring manage destruction of Prototype Beans?
No. Spring manages creation and initialization but not destruction of Prototype Beans.
Professional Interview Answer
Prototype Bean is a bean scope in Spring Framework where a new bean instance is created every time the bean is requested from the Spring IoC container. Unlike Singleton scope, Prototype Beans are not shared and each request receives an independent object. Prototype scope is useful for stateful objects, temporary processing components, and request-specific operations in enterprise applications and microservices architectures.
Conclusion
Prototype Beans are an important bean scope in Spring Framework and Spring Boot used for creating independent object instances whenever required.
They are especially useful in enterprise systems that require stateful processing, request-specific operations, temporary data handling, and isolated object management.
Although Singleton scope is the default and most commonly used scope, Prototype scope becomes extremely valuable for dynamic processing and stateful operations in banking systems, e-commerce platforms, and cloud-native applications.
Understanding Prototype Beans is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.