Spring prototype scope - Use Cases?

19.7k Views Asked by At

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).

4

There are 4 best solutions below

3
On

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.

0
On

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 create new 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 think 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.

1
On

I have used prototype mostly in conjunction with spring lookup-method. My application is a game server that needs to decode incoming bytes at tcp port. Consider the following bean definition

<bean id="channelBufferProtocol" class="org.menacheri.protocols.impl.ChannelBufferProtocol">
    <lookup-method name="createLengthBasedFrameDecoder" bean="lengthFieldBasedFrameDecoder"/>
    <property name="eventDecoder" ref="eventDecoder"></property>
    <property name="lengthFieldPrepender" ref="lengthFieldPrepender"></property>
    <property name="eventEncoder" ref="eventEncoder"></property>
</bean>

Inside the protocol implementation class, I have the following code to create the frame decoder pipeline.addLast("lengthDecoder", createLengthBasedFrameDecoder()); When this method is invoked, spring will create a new frame decoder instance and return it.

The bean returned by bean="lengthFieldBasedFrameDecoder" needs to be of scope prototype, since it is a stateful bean in my app.

Note: A protocol is nothing but a specific set of decoders and encoders chained together. "Chain of responsibility" design pattern.

0
On

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