原型范围会引起多少额外开销?

huangapple go评论146阅读模式
英文:

How much overhead does prototype scope cause?

问题

我的理解是,原型范围的Java bean类每次请求时都会实例化,与单例范围的Java bean不同,后者在应用程序启动时只实例化一次。

我的问题是:使用原型范围会导致多少额外开销?

  • Spring框架是否尝试回收原型范围对象,以减少重复实例化造成的开销?
  • 原型范围引起的开销是否足以值得努力使我尽量使我的Java bean类无状态,以便可以将单例范围应用于它们?
英文:

My understanding is that a prototype-scoped Java bean class is instantiated every time it is requested, as opposed to singleton-scoped Java beans which are instantiated once when the application starts up.

My question is: how much overhead does using the prototype scope cause?

  • Does the Spring framework make any attempt to recycle prototype-scoped objects to reduce the overhead caused by repeated instantiation?
  • Is the overhead caused by prototype scoping enough to warrant efforts to make as many of my Java bean classes stateless so that I can apply singleton scope to them?

答案1

得分: 2

Using prototype scope is the same as if you were to instantiate using 'new' except that the new object will be managed by Spring. Spring will be able to apply aspects, inject dependencies, etc.. There will also be the overhead of the proxy class created to wrap the object.

Spring will not attempt to reuse the instances.

If you do not want to create a new instance whenever you reference the bean then you should not be using a prototype. Your solution will either require independent instances, maybe due to concurrency, or it will allow for a singleton. It is unlikely that the memory used by the object will be the deciding factor for what scope you use.

英文:

Using prototype scope is the same as if you were to instantiate using 'new' except that the new object will be managed by Spring. Spring will be able to apply aspects, inject dependencies, etc.. There will also be the overhead of the proxy class created to wrap the object.

Spring will not attempt to reuse the instances.

If you do not want to create a new instance whenever you reference the bean then you should not be using a prototype. Your solution will either require independent instances, maybe due to concurrency, or it will allow for a singleton. It is unlikely that the memory used by the object will be the deciding factor for what scope you use.

答案2

得分: 1

不应该考虑原型和单例范围之间的开销,只应考虑它们的使用方式。如果它们是无状态对象,将在应用程序的整个生命周期内存在,那么正确的范围是单例。许多bean存在于该范围内,例如控制器、服务或持久性层中的bean。与Spring Security框架相关的大多数bean也是无状态的,并且位于单例范围内。

如果您想为特定处理创建一个全新的bean,然后在使用后丢弃它,那么这是原型范围的用例。由于我主要在Web应用程序中使用Spring,我很少使用原型范围的bean,而是使用了与之类似的请求范围的bean:它们是为HTTP请求处理而创建的。唯一需要注意的是,如果您必须在单例bean中注入原型或请求范围的bean,您将需要使用允许连接到当前原型bean(可能在线程存储中)的代理,而不是在单例创建时创建的bean。

英文:

You should not think of overhead between the prototype and singleton scope, but only the way they are used. If they are stateless objects that will live throughout the life time of the application, the correct scope is singleton. Many beans exist in that scope, for example controllers, beans in a service or persistence layer. Most beans related to Spring Security framework are also stateless and live in singleton scope.

If you want to create a brand new bean for a specific processing and then discard it when it has been used, then it is a use case for the prototype scope. As I mainly used spring for Web application I seldom used prototype scoped beans, but used request scoped beans which have a close use case: they are created for a HTTP request processing. The only point of attention is that if you have to inject a prototype or request scoped bean in a singleton bean, you will have to use a proxy that allows to connect to the current prototype bean (may be in thread storage) and not a bean that would have been created when the singleton was.

huangapple
  • 本文由 发表于 2020年7月31日 22:44:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63194067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定