views:

1671

answers:

5

Being a complete turbot this afternoon and cant seem to find the answer anywhere.

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.

Thanks in advance.

Karl

+3  A: 

Yes, use JNDI lookup.

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

Bozho
+2  A: 

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
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
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
A: 

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.

deamon
A: 

This is another article on the subject: http://www.adam-bien.com/roller/abien/entry/how_to_unit_test_ejb

Jan
A: 

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