何时应该实例< T>和提供者< T>用于在CDI注入豆类?

编程入门 行业动态 更新时间:2024-10-16 08:30:34
本文介绍了何时应该实例< T>和提供者< T>用于在CDI注入豆类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在阅读的API文档实例< T> 和 提供商< T> ,但是应该何时使用它们并不完全清楚。

I've been reading the API documentation of Instance<T> and Provider<T>, but it isn't completely clear when they should be used.

以下方法之间的区别是什么?

What's the difference between the following approaches?

@Inject MyBean bean;

@Inject Instance<MyBean> bean;

@Inject Provider<MyBean> bean;

推荐答案

提供商< T> 是一个JSR-330接口,由CDI接口扩展 Instance< T> 。

Provider<T> is a JSR-330 interface which is extended by the CDI interface Instance<T>.

注入 MyBean ,当没有匹配的bean或多个匹配的bean时,你的应用程序将在启动期间抛出异常。

Injecting MyBean, your application will throw an exception during startup when there is no matching bean or more than one matching bean.

注入实例< MyBean> ,将bean解析委托给应用程序:您可以遍历所有候选bean并且 select()你想要的那个或者叫 isUnsatisfied()并决定当没有匹配的bean时该怎么办。

Injecting Instance<MyBean>, bean resolution is delegated to the application: you can iterate over all candidate beans and select() the one you want or call isUnsatisfied() and decide what to do when there is no matching bean.

对于 @Dependent 范围的bean,调用 Instance.get()将为每个bean创建一个新实例调用,当你不再需要它时,你应该为每个这样的实例调用 Instance.destroy(t)。

For beans with @Dependent scope, calling Instance.get() will create a new instance for each invocation, and you should invoke Instance.destroy(t) for each such instance when you no longer need it.

提供商只有 get()方法,但没有 des troy()或 select()并且不支持迭代。在CDI环境中,对于 Provider< T> 解决的任何用例,最好使用 Instance< T> 代替。

Provider just has the get() method, but no destroy() or select() and does not support iteration. In a CDI environment, for any use case addressed by Provider<T>, you had better use Instance<T> instead.

更多推荐

何时应该实例&lt; T&gt;和提供者&lt; T&gt;用于在CDI注入豆类?

本文发布于:2023-11-02 16:59:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:豆类   提供者   实例   amp   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!