Java EJB 3.1 @EJB 注入 POJO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2021370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EJB 3.1 @EJB Injection into POJO
提问by Karl
With the new EJB 3.1 spec is it possible to inject an EJB into a pojo? I know in EJB 3.0 the @EJB annotation could be used to inject an EJB but this did not work on simple pojos.
使用新的 EJB 3.1 规范是否可以将 EJB 注入 pojo?我知道在 EJB 3.0 中,@EJB 注释可用于注入 EJB,但这在简单的 pojo 上不起作用。
If it is not do I have to look the bean up in JNDI as I know you cannot simple use the new keyword.
如果不是,我必须在 JNDI 中查找 bean,因为我知道您不能简单地使用 new 关键字。
采纳答案by Bozho
Yes, use JNDI lookup.
是的,使用 JNDI 查找。
Since your POJO is created by you (I assume), the container is not responsible for injecting the dependencies.
由于您的 POJO 是由您创建的(我假设),因此容器不负责注入依赖项。
回答by Laird Nelson
The new EJB spec (3.1) adds the ability to specify global JNDI names for EJBs. This means that you can use them in any bean, anywhere.
新的 EJB 规范 (3.1) 添加了为 EJB 指定全局 JNDI 名称的能力。这意味着您可以在任何 bean 的任何地方使用它们。
You must do an explicit JNDI lookup, however, as an EJB 3.1 container will not know about your POJO.
但是,您必须执行显式 JNDI 查找,因为 EJB 3.1 容器不会知道您的 POJO。
The only exception, which I'm guessing does not apply to you, is if your POJO is really an application client, in which case provided the field that is to contain the EJB is static, you may apply the @EJB annotation to it. If that's your situation, you should check out the application client rules in the overall Java EE 5 specification.
我猜这不适用于您的唯一例外是,如果您的 POJO 确实是一个应用程序客户端,在这种情况下,如果包含 EJB 的字段是静态的,您可以对其应用 @EJB 注释。如果这是您的情况,您应该查看整个 Java EE 5 规范中的应用程序客户端规则。
Finally, Java EE 6, with its inclusion of JSR-299, may allow what you describe to happen in some way; I do not know the spec yet so cannot comment on it.
最后,包含 JSR-299 的 Java EE 6 可能允许您所描述的以某种方式发生;我还不知道规范,所以不能评论它。
I hope this all helps.
我希望这一切都有帮助。
回答by deamon
I wonder too if I could inject EJBs into unmanaged objects. See the Weld(JSR 299 reference implementation) documentation for more details.
我也想知道是否可以将 EJB 注入非托管对象。有关更多详细信息,请参阅Weld(JSR 299 参考实现)文档。
But you can perform dependency injection by hand inside a repository or factory like this:
但是您可以在存储库或工厂中手动执行依赖注入,如下所示:
@Stateless
public PojoRespository {
@Inject
ResourceForPojos resource;
@PersistenceContext
private EntityManager em;
public Pojo findById(Object id) {
Pojo p = (Pojo) em.find(Pojo.class, id);
p.setResource(resource); // injects resource
return p;
}
}
If you have many methods where injection should be performed, you could use an interceptor.
如果您有许多方法需要执行注入,则可以使用拦截器。
回答by Pascal Thivent
With the new EJB 3.1 spec is it possible to inject an EJB into a pojo? I know in EJB 3.0 the @EJB annotation could be used to inject an EJB but this did not work on simple pojos.
使用新的 EJB 3.1 规范是否可以将 EJB 注入 pojo?我知道在 EJB 3.0 中,@EJB 注释可用于注入 EJB,但这在简单的 pojo 上不起作用。
Injection of EJB into an POJO is possible IFyou use JSR-299 (Java Contexts and Dependency Injection) i.e. if your POJO is a CDI managed bean. In that case, you could do:
EJB注射到POJO可能如果您使用JSR-299(Java的çontexts和dependency我njection),即如果你的POJO是一个CDI托管bean。在这种情况下,你可以这样做:
@Inject MyEJB service
But this is not an EJB 3.1 feature, this comes from CDI. And if you're not using CDI, you'll have to do a lookup.
但这不是 EJB 3.1 特性,它来自 CDI。如果您不使用 CDI,则必须进行查找。