Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring prototype scope - Use Cases?

I have clear understanding of the various scopes of Spring beans. But I am looking for some use cases of prototype scope of a bean in enterprise tier projects. It would be great if you can share some real life use cases of the prototype scope (not the request scope).

like image 703
Tarun Sapra Avatar asked Mar 12 '12 09:03

Tarun Sapra


People also ask

What is the use case for prototype scope in Spring?

We can use prototype scope in case of model classes(also called as Entities in hibernate) as application need different instances of model class for each thread/request. Then shouldn't it be request scoped instead of prototype scoped? A request can have multiple instances of the same entity.

In which scenario prototype scope is used?

Prototype Scope: A request can be made to the bean instance either programmatically using getBean() method or by XML for Dependency Injection of secondary type. Generally, we use the prototype scope for all beans that are stateful, while the singleton scope is used for the stateless beans.

What is difference between request and prototype scope in Spring?

Prototype scope creates a new instance every time getBean method is invoked on the ApplicationContext. Whereas for request scope, only one instance is created for an HttpRequest.

What is a prototype scope?

prototype. Scopes a single bean definition to any number of object instances. request. Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition.


2 Answers

As someone who previously worked at SpringSource and have talked to the developers on this topic. Here is my take. Prototype is great for testing things out, hence the name prototype and not createnew or something more description of creating a new instance of the bean each and every time you request it from the Spring container.

I have also found in my use over the years that I cannot thing of any other place where prototype makes sense in any real world production application. If your object holds state, it typically shouldn't be a Spring bean. I have found in all the applications I have worked on that all beans are Services, Repositories, and Singleton non state holding objects where I need to add features like Transactionality, JPA, JMS and the likes that give us the enterprise features that POJOs don't have.

The objects in my system that hold state are my Entities and View DTOs maybe, or other things that just make no sense to be a Spring Bean. So therefore in my applications in production there hasn't been a single "prototype" bean.

like image 155
user1567291 Avatar answered Sep 23 '22 12:09

user1567291


I used prototype beans to declare configured form elements (a textbox configured to validate names, e-mail addresses for example) and get "living" instances of them for every form being created in my webapp. The details are not important, only the principle, that I would summarize this way:

  • There is a class that has many config parameters
  • You need to create instances of it with a set of predefined configuration (fancy1, fancy2, stc.)
  • Think of the applicationContext.getBean("myBeanConfiguredFancy1") as a kind of factory method that creates the instance as preconfigured in the xml
like image 45
jabal Avatar answered Sep 21 '22 12:09

jabal