gpt4 book ai didi

java - Spring Prototype 作用域和 CDI 依赖作用域有什么区别?

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:59 25 4
gpt4 key购买 nike

Spring原型(prototype)作用域是否与CDI依赖作用域相同。

谷歌搜索让我找到声称它们相同的博客文章,以及其他声称它们相似但不完全相同但没有解释差异的博客文章。

那么spring原型(prototype)作用域和cdi依赖作用域有什么区别呢?

最佳答案

根据 CDI documentationjavadoc

When a bean is declared to have @Dependent scope:

  • No injected instance of the bean is ever shared between multiple injection points.
  • ...

同样,Spring documentation

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 的生命周期。 In Spring

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.

In CDI however容器管理 bean 的整个生命周期,当它作为方法调用参数注入(inject)时直接管理,或者在销毁注入(inject)它的 bean 时间接管理。这些条件都在链接的文档中进行了描述。

正如 Luiggi 在评论中提到的,注意 bean 声明的默认范围很重要。 In the Spring docs state

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

while in CDI, the default scope is dependent.

关于java - Spring Prototype 作用域和 CDI 依赖作用域有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20039151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com