8

Since Java EE 5 does not have CDI, how does DI work in here?

Can an EJB be injected with @EJB in a class that is a non-EJB? Can an EntityManager be injected with @PersistenceContext in a class that is a non-EJB (container managed not resource local EntityManager)? Or the only solution to bring the resources in non-EJB classes is by JNDI lookup?

By non-EJB class I mean a class that is not annotated with @Stateless/@Statefull or others.

tibtof
  • 7,857
  • 1
  • 32
  • 49
Random42
  • 8,989
  • 6
  • 55
  • 86
  • 1
    Bear in mind that there are pre-CDI frameworks you can use in this situation - Guice, PicoContainer, even the dreaded Spring. They don't have an natural ability to inject EJBs or the contextual `EntityManager` into their objects, but it should be fairly simple to build a bridge which would let them do so, although this margin is too small to contain an explanation of that. – Tom Anderson Nov 23 '12 at 14:38

1 Answers1

5

Taken from: http://www.oracle.com/technetwork/articles/javaee/injection-141192.html

Keep in mind that a Java EE 5 platform container can handle the injections transparently only when they are used on container-managed components, such as EJB beans, Servlets, and JavaServer Pages (JSP) technology tag handlers.

onjava has an article: http://onjava.com/pub/a/onjava/2006/01/04/dependency-injection-java-ee-5.html

It presents a readable table of which managed-components (per container) that support injection of what type of resources.

Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
  • Suppose I have an EJB (POJO annotated with @Stateless) that has some fields that are container-managed components, say an EntityManager annotated with @PersistenceContext and other EJB annotated with @EJB/ If I want to use the functionality of this EJB in a non container managed class I can look it up via JNDI; but will the EntityManager and the other EJB be injected and ready to be used? – Random42 Nov 25 '12 at 12:21
  • I believe so, you experiencing different behaviour? – Aksel Willgert Nov 25 '12 at 12:40