13

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.

If it is not do I have to look the bean up in JNDI as I know you cannot simple use the new keyword.

Hash
  • 4,647
  • 5
  • 21
  • 39
Karl
  • 2,927
  • 8
  • 31
  • 39

4 Answers4

29

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.

Injection of EJB into an POJO is possible IF you use JSR-299 (Java Contexts and Dependency Injection) i.e. if your POJO is a CDI managed bean. In that case, you could do:

@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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
19

Yes, use JNDI lookup.

Since your POJO is created by you (I assume), the container is not responsible for injecting the dependencies.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Not all the implementations for Java EE 5 allow for a @Local bean to be published in the JNDI tree. As far as I can remember that is an optional feature in the spec. WebLogic 11g doesn't have that going on, so in that case we pass the injected resource as a constructor argument to the POJO. – Evandro Pomatti Mar 26 '15 at 11:51
7

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.

You must do an explicit JNDI lookup, however, as an EJB 3.1 container will not know about your 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.

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.

I hope this all helps.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
  • 1
    Performing a lookup from anywhere was already possible with previous versions of Java EE (even J2EE). EJB 3.1 doesn't change that. What's new in Java EE 6 is that you can make anything a managed bean using CDI and also benefit from injection in CDI beans. – Pascal Thivent Aug 14 '10 at 14:21
  • 1
    Previous versions of Java EE did not specify the global JNDI naming syntax. Nor did they make it a requirement that a remote client that was not an application client be able to look up anything in JNDI. – Laird Nelson Aug 14 '10 at 19:54
1

I wonder too if I could inject EJBs into unmanaged objects. See the Weld (JSR 299 reference implementation) documentation for more details.

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.

Community
  • 1
  • 1
deamon
  • 89,107
  • 111
  • 320
  • 448