Java Spring 原型范围 - 用例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9664810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring prototype scope - Use Cases?
提问by Tarun Sapra
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 prototypescope (not the requestscope).
我对 Spring bean 的各个范围有清楚的了解。但我正在寻找企业层项目中 bean 原型范围的一些用例。如果您可以分享原型范围(而不是请求范围)的一些实际用例,那就太好了。
回答by Abe
I have used prototype mostly in conjunction with spring lookup-method
. My application is a game serverthat needs to decode incoming bytes at tcp port. Consider the following bean definition
我主要将原型与 spring 结合使用lookup-method
。我的应用程序是一个游戏服务器,需要在 tcp 端口解码传入的字节。考虑以下 bean 定义
<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.
在协议实现类里面,我有下面的代码来创建帧解码器pipeline.addLast("lengthDecoder", createLengthBasedFrameDecoder());
当这个方法被调用时,spring会创建一个新的帧解码器实例并返回它。
The bean returned by bean="lengthFieldBasedFrameDecoder"
needs to be of scope prototype
, since it is a stateful bean in my app.
返回的 beanbean="lengthFieldBasedFrameDecoder"
需要是 scope prototype
,因为它在我的应用程序中是一个有状态的 bean。
Note:A protocol is nothing but a specific set of decoders and encoders chained together. "Chain of responsibility" design pattern.
注意:协议只不过是链接在一起的一组特定的解码器和编码器。“责任链”设计模式。
回答by jabal
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:
我使用原型 bean 来声明已配置的表单元素(一个配置为验证姓名、电子邮件地址的文本框),并为我的 web 应用程序中创建的每个表单获取它们的“活动”实例。细节不重要,重要的是原则,我会这样总结:
- 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 methodthat creates the instance as preconfigured in the xml
- 有一个类有很多配置参数
- 您需要使用一组预定义的配置(fancy1、fancy2、stc)创建它的实例。
- 将其
applicationContext.getBean("myBeanConfiguredFancy1")
视为一种工厂方法,可以创建在 xml 中预先配置的实例
回答by Rupesh Deo
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.
我们可以在模型类(在休眠中也称为实体)的情况下使用原型范围,因为应用程序需要每个线程/请求的模型类的不同实例。
回答by user1567291
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.
作为之前在 SpringSource 工作并与开发人员就这个话题进行过交谈的人。这是我的看法。Prototype 非常适合测试事物,因此名称为原型而不是 createnew 或更多描述每次从 Spring 容器请求时创建 bean 的新实例。
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.
我还发现在我多年来的使用中,我无法想象原型在任何现实世界的生产应用程序中有意义的任何其他地方。如果您的对象保持状态,则它通常不应该是 Spring bean。我发现在我处理过的所有应用程序中,所有 bean 都是服务、存储库和单例非状态持有对象,我需要在其中添加诸如事务性、JPA、JMS 之类的功能,这些功能为我们提供了 POJO 不具备的企业功能没有。
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.
我的系统中保存状态的对象可能是我的实体和视图 DTO,或者其他对成为 Spring Bean 毫无意义的东西。因此,在我的生产应用程序中,没有一个“原型”bean。