Reputation: 79
Can someone explain the difference between Spring “prototype” bean scope and using “new” operator? Also what is the advantage of declaring the bean with “prototype” scope over “new” operator?
Upvotes: 2
Views: 1536
Reputation: 9622
Prototype beans do mean that a new instance of the bean is created every time it is requested (in which case you might think you may as well be instantiating it yourself when needed using new
).
But the key thing is that they still satisfy the dependency injection design pattern (https://en.wikipedia.org/wiki/Dependency_injection) which among many other things makes unit testing with mocked dependencies significantly easier.
Upvotes: 7