我应该使用@EJB还是@Inject [英] Should I use @EJB or @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使用这个注释时,EJB容器的注入是否使用新的CDI框架?是否有区别,如果是这样,我应该使用@Inject而不是@EJB?
Is @EJB and 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
的属性,如 beanName
,查找
或 beanInterface
)比使用 @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:
Antonio Goncalves博客:
CDI第一部分
CDI Part II
CDI第三部分
Antonio Goncalves' blog:
CDI Part I
CDI Part II
CDI Part III
JBoss焊接文档:
CD我和Java EE生态系统
JBoss Weld documentation:
CDI and the Java EE ecosystem
StackOverflow:
根据条件注入@EJB bean
StackOverflow:
Inject @EJB bean based on conditions
这篇关于我应该使用@EJB还是@Inject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!