Spring prototype scope - Use Cases?
Asked Answered
A

4

47

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

Avidin answered 12/3, 2012 at 9:49 Comment(1)
I am surprised this short question with no code in it, has not been marked down as not constructive, too broad. don't get me wrong, I think this question is greatCharpentier
D
27

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.

Dominoes answered 20/4, 2016 at 17:49 Comment(0)
L
10

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
Lanna answered 12/3, 2012 at 12:46 Comment(0)
A
4

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.

Acidulate answered 12/3, 2012 at 10:40 Comment(1)
This lookup-method is explained by Spring doc: docs.spring.io/spring/docs/current/spring-framework-reference/…Diffluent
C
1

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.

Cosgrove answered 12/10, 2015 at 13:42 Comment(3)
Then shouldn't it be request scoped instead of prototype scoped?Lenard
A request can have multiple instances of the same entity.Jealousy
When would that make sense?Hotien

© 2022 - 2024 — McMap. All rights reserved.