Spring 请求和原型范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6480005/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:34:14  来源:igfitidea点击:

Spring Request and Prototype Scope?

spring

提问by M Sach

Below are the definitions of prototype and request scope in Spring.

下面是 Spring 中原型和请求范围的定义。

prototype Scopes a single bean definition to any number of object instances.

原型将单个 bean 定义范围限定为任意数量的对象实例。

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. Only valid in the context of a web-aware Spring ApplicationContext.

request 将单个 bean 定义范围限定为单个 HTTP 请求的生命周期;也就是说,每个 HTTP 请求都有自己的 bean 实例,该 bean 实例是在单个 bean 定义的后面创建的。仅在 web-aware Spring ApplicationContext 的上下文中有效。

As per my understanding In case of prototype scope , some pool will be maintained by core container. It will serve the bean instance from that pool. In case of request scope, new bean will be served for each http request. Please correct me if there is some dicrepency in understanding?

根据我的理解,在原型范围的情况下,核心容器将维护一些池。它将为该池中的 bean 实例提供服务。在请求范围的情况下,将为每个 http 请求提供新的 bean。如果在理解上有一些分歧,请纠正我?

If above statements are true, then if bean is holding some state then the scope should not be defined as prototype rather it should be defined as request. Correct?

如果上述陈述为真,那么如果 bean 持有某种状态,则不应将范围定义为原型,而应将其定义为请求。正确的?

回答by KARTHIK

Best one i found on net

我在网上找到的最好的

Prototype creates a brand new instance everytime you call getBean on the ApplicationContext. Whereas for Request, only one instance is created for an HttpRequest. So in a single HttpRequest, I can call getBean twice on Application and there will only ever be one bean instantiated, whereas that same bean scoped to Prototype in that same single HttpRequest would get 2 different instances.

每次在 ApplicationContext 上调用 getBean 时,Prototype 都会创建一个全新的实例。而对于 Request,只为 HttpRequest 创建一个实例。因此,在单个 HttpRequest 中,我可以在 Application 上调用 getBean 两次,并且只会实例化一个 bean,而在同一个 HttpRequest 中作用域为 Prototype 的同一个 bean 将获得 2 个不同的实例。

HttpRequest scope

HttpRequest 范围

Mark mark1 = context.getBean("mark"); 
Mark mark2 = context.getBean("mark"); 
mark1 == mark2; //This will return true 

Prototype scope

原型范围

Mark mark1 = context.getBean("mark"); 
Mark mark2 = context.getBean("mark"); 
mark1 == mark2; //This will return false 

Hope that clears it up for you.

希望这可以为您解决问题。

回答by hvgotcodes

You are off. Prototype is described in the docs hereas

你没电了 原型在此处的文档中描述为

"The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made."

“bean 部署的非单一原型范围导致每次对特定 bean 发出请求时都会创建一个新的 bean 实例。”

Your description of request scoped beans is accurate.

您对请求范围 bean 的描述是准确的。

Probably just got the wires crossed vis-a-vis prototype vs singleton.

可能只是让电线与原型与单身人士交叉。

回答by Jimesh Shah

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.

每次在 ApplicationContext 上调用 getBean 方法时,原型作用域都会创建一个新实例。而对于请求范围,只为 HttpRequest 创建一个实例。

So in an HttpRequest, if the getBean method is called twice on Application and there will be only one bean instantiated and reused, whereas the bean scoped to Prototype in that same single HttpRequest would get 2 different instances.

因此,在 HttpRequest 中,如果在 Application 上两次调用 getBean 方法,并且只会实例化和重用一个 bean,而在同一个 HttpRequest 中作用域为 Prototype 的 bean 将获得 2 个不同的实例。