简体   繁体   English

Spring原型范围-用例?

[英]Spring prototype scope - Use Cases?

I have clear understanding of the various scopes of Spring beans. 我对Spring bean的各种范围有清楚的了解。 But I am looking for some use cases of prototype scope of a bean in enterprise tier projects. 但是我正在寻找企业级项目中bean原型范围的一些用例。 It would be great if you can share some real life use cases of the prototype scope (not the request scope). 如果您可以共享原型范围(而不是请求范围)的一些现实生活用例,那就太好了。

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. 我使用原型bean来声明配置的表单元素(配置为验证名称,电子邮件地址的文本框),并为在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.) 您需要使用一组预定义的配置(fancy1,fancy2,stc)创建它的实例。
  • Think of the applicationContext.getBean("myBeanConfiguredFancy1") as a kind of factory method that creates the instance as preconfigured in the xml applicationContext.getBean("myBeanConfiguredFancy1")视为一种工厂方法 ,该方法创建在xml中预先配置的实例

As someone who previously worked at SpringSource and have talked to the developers on this topic. 作为以前在SpringSource工作过并与开发人员就此主题进行过交谈的人。 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. 原型非常适合测试事物,因此,每次您从Spring容器中请求原型时,都应使用原型名称而不是createnew或更多有关创建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. 如果您的对象保持状态,则通常不应将其作为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. 我在所有工作的应用程序中发现,所有bean都是服务,存储库和Singleton非状态持有对象,我需要在其中添加诸如Transactionality,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. 我系统中保持状态的对象可能是我的实体和视图DTO,或者是其他没有意义的对象,例如Spring Bean。 So therefore in my applications in production there hasn't been a single "prototype" bean. 因此,在我的生产应用程序中,没有一个“原型” bean。

I have used prototype mostly in conjunction with spring lookup-method . 我将原型主要与spring lookup-method结合使用。 My application is a game server that needs to decode incoming bytes at tcp port. 我的应用程序是一个游戏服务器 ,需要解码tcp端口上的传入字节。 Consider the following bean definition 考虑下面的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()); 在协议实现类中,我具有以下代码来创建帧解码器pipeline.addLast("lengthDecoder", createLengthBasedFrameDecoder()); When this method is invoked, spring will create a new frame decoder instance and return it. 调用此方法时,spring将创建一个新的帧解码器实例并将其返回。

The bean returned by bean="lengthFieldBasedFrameDecoder" needs to be of scope prototype , since it is a stateful bean in my app. bean="lengthFieldBasedFrameDecoder"返回的bean="lengthFieldBasedFrameDecoder"必须是范围prototype ,因为它在我的应用程序中是有状态的bean。

Note: A protocol is nothing but a specific set of decoders and encoders chained together. 注意:协议不过是一组链接在一起的特定解码器和编码器。 "Chain of responsibility" design pattern. “责任链”的设计模式。

对于模型类(在休眠中也称为Entities),我们可以使用原型范围,因为应用程序需要为每个线程/请求使用不同的模型类实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM