Spring Prototype范围和CDI依赖范围之间有什么区别?

时间:2021-03-27 13:32:31

Is the Spring prototype scope the same as the CDI dependant scope.

Spring原型范围是否与CDI相关范围相同。

Googling lead me to blog posts that claimed they are the same and others that claimed that they are similar but not quite the same without explaining the differences.

谷歌搜索引导我发布声称他们是相同的博客帖子和其他声称他们相似但不完全相同而没有解释差异的人。

So what are the differences between the spring prototype scope and the cdi dependant scope?

那么弹簧原型范围和cdi依赖范围之间有什么区别?

1 个解决方案

#1


5  

According to the CDI documentation and javadoc

根据CDI文档和javadoc

When a bean is declared to have @Dependent scope:

当bean被声明为具有@Dependent范围时:

  • No injected instance of the bean is ever shared between multiple injection points.
  • 在多个注入点之间不共享注入的bean实例。
  • ...
  • ...

Similarly, the Spring documentation states

同样,Spring文档指出

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实例。

They are, behaviorally, the same.

从行为上讲,它们是一样的。

The only difference I could find is on the lifecycle of the bean. In Spring

我能找到的唯一区别是bean的生命周期。在春天

Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.

因此,尽管无论范围如何都在所有对象上调用初始化生命周期回调方法,但在原型的情况下,不会调用已配置的销毁生命周期回调。客户端代码必须清理原型范围的对象并释放原型bean所持有的昂贵资源。

In CDI however the container manages the full lifecycle of the bean, either directly when it is injected as a method invocation argument or indirectly when destroying the bean it was injected into. The conditions are all described in the documentation linked.

然而,在CDI中,容器管理bean的整个生命周期,或者直接在它作为方法调用参数注入时,或者在销毁它被注入的bean时间接管理。这些条件都在链接的文档中描述。

As Luiggi mentions in the comments, it is important to note the default scope of a bean declaration. In the Spring docs state

正如Luiggi在评论中提到的那样,重要的是要注意bean声明的默认范围。在Spring docs状态中

The singleton scope is the default scope [...]

单例范围是默认范围[...]

while in CDI, the default scope is dependent.

而在CDI中,默认范围是相关的。

#1


5  

According to the CDI documentation and javadoc

根据CDI文档和javadoc

When a bean is declared to have @Dependent scope:

当bean被声明为具有@Dependent范围时:

  • No injected instance of the bean is ever shared between multiple injection points.
  • 在多个注入点之间不共享注入的bean实例。
  • ...
  • ...

Similarly, the Spring documentation states

同样,Spring文档指出

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实例。

They are, behaviorally, the same.

从行为上讲,它们是一样的。

The only difference I could find is on the lifecycle of the bean. In Spring

我能找到的唯一区别是bean的生命周期。在春天

Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding.

因此,尽管无论范围如何都在所有对象上调用初始化生命周期回调方法,但在原型的情况下,不会调用已配置的销毁生命周期回调。客户端代码必须清理原型范围的对象并释放原型bean所持有的昂贵资源。

In CDI however the container manages the full lifecycle of the bean, either directly when it is injected as a method invocation argument or indirectly when destroying the bean it was injected into. The conditions are all described in the documentation linked.

然而,在CDI中,容器管理bean的整个生命周期,或者直接在它作为方法调用参数注入时,或者在销毁它被注入的bean时间接管理。这些条件都在链接的文档中描述。

As Luiggi mentions in the comments, it is important to note the default scope of a bean declaration. In the Spring docs state

正如Luiggi在评论中提到的那样,重要的是要注意bean声明的默认范围。在Spring docs状态中

The singleton scope is the default scope [...]

单例范围是默认范围[...]

while in CDI, the default scope is dependent.

而在CDI中,默认范围是相关的。