我应该使用@EJB 还是@Inject [英] Should I use @EJB or @Inject

查看:26
本文介绍了我应该使用@EJB 还是@Inject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这个问题:@Inject 有什么区别和@EJB 但我没有更明智.我之前没有用过 Java EE,也没有依赖注入的经验,所以我不明白我应该使用什么?

I have found this question: What is the difference between @Inject and @EJB but I did not get any wiser. I have not done Java EE before nor do I have experience with dependency injection so I do not understand what I should use?

@EJB 是一种旧的注入方式吗?在使用@Inject 使用新的CDI 框架时,使用此注解时是否由EJB 容器完成注入?这是区别吗?如果是这种情况,我应该使用 @Inject 而不是 @EJB 吗?

Is @EJB an old way of injecting? Is the injection done by the EJB container when using this annotation while using @Inject use the new CDI framework? Is that the difference and should I be using @Inject instead of @EJB if this is the case?

推荐答案

@EJB 仅用于注入 EJB,现在已经有一段时间了.@Inject 可以注入任何托管 bean,并且是新 CDI 规范的一部分(自 Java EE 6).

The @EJB is used to inject EJB's only and is available for quite some time now. @Inject can inject any managed bean and is a part of the new CDI specification (since Java EE 6).

在简单的情况下,您可以简单地将 @EJB 更改为 @Inject.在更高级的情况下(例如,当您严重依赖 @EJB 的属性,如 beanNamelookupbeanInterface) 而为了使用 @Inject 你需要定义一个 @Producer 字段或方法.

In simple cases you can simply change @EJB to @Inject. In more advanced cases (e.g. when you heavily depend on @EJB's attributes like beanName, lookup or beanInterface) than in order to use @Inject you would need to define a @Producer field or method.

这些资源可能有助于理解 @EJB@Produces 之间的区别以及如何充分利用它们:

These resources might be helpful to understand the differences between @EJB and @Produces and how to get the best of them:

安东尼奥·贡萨尔维斯的博客:
CDI 第一部分
CDI 第二部分
CDI 第三部分

Antonio Goncalves' blog:
CDI Part I
CDI Part II
CDI Part III

JBoss Weld 文档:
CDI 和 Java EE 生态系统

JBoss Weld documentation:
CDI and the Java EE ecosystem

StackOverflow:
根据条件注入@EJB bean

StackOverflow:
Inject @EJB bean based on conditions

这篇关于我应该使用@EJB 还是@Inject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆