简体   繁体   English

EJB 3.1 @EJB注入POJO

[英]EJB 3.1 @EJB Injection into POJO

With the new EJB 3.1 spec is it possible to inject an EJB into a pojo? 使用新的EJB 3.1规范是否可以将EJB注入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.0中,@ EJB注释可用于注入EJB,但这对简单的pojos不起作用。

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关键字。

With the new EJB 3.1 spec is it possible to inject an EJB into a pojo? 使用新的EJB 3.1规范是否可以将EJB注入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.0中,@ EJB注释可用于注入EJB,但这对简单的pojos不起作用。

Injection of EJB into an POJO is possible IF you use JSR-299 (Java C ontexts and D ependency I njection) ie if your POJO is a CDI managed bean. 如果您使用JSR-299(Java C ontexts和D ependency I njection),即如果您的POJO是CDI托管bean,则可以将EJB注入POJO。 In that case, you could do: 在这种情况下,你可以这样做:

@Inject MyEJB service

But this is not an EJB 3.1 feature, this comes from CDI. 但这不是EJB 3.1功能,它来自CDI。 And if you're not using CDI, you'll have to do a lookup. 如果您不使用CDI,则必须进行查找。

Yes, use JNDI lookup. 是的,使用JNDI查找。

Since your POJO is created by you (I assume), the container is not responsible for injecting the dependencies. 由于您的POJO是由您创建的(我假设),因此容器不负责注入依赖项。

The new EJB spec (3.1) adds the ability to specify global JNDI names for EJBs. 新的EJB规范(3.1)增加了为EJB指定全局JNDI名称的功能。 This means that you can use them in any bean, anywhere. 这意味着您可以在任何地方的任何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. 唯一的例外,我猜不适用于你,如果你的POJO真的是一个应用程序客户端,在这种情况下,提供包含EJB的字段是静态的,你可以将@EJB注释应用于它。 If that's your situation, you should check out the application client rules in the overall Java EE 5 specification. 如果这是您的情况,您应该检查整个Java EE 5规范中的应用程序客户端规则。

Finally, Java EE 6, with its inclusion of JSR-299, may allow what you describe to happen in some way; 最后,包含JSR-299的Java EE 6可能允许您描述的内容以某种方式发生; I do not know the spec yet so cannot comment on it. 我不知道规格,所以不能评论它。

I hope this all helps. 我希望这一切都有帮助。

I wonder too if I could inject EJBs into unmanaged objects . 我也想知道是否可以将EJB注入非托管对象 See the Weld (JSR 299 reference implementation) documentation for more details. 有关更多详细信息,请参阅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. 如果您有许多应该执行注射的方法,则可以使用拦截器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM