Search code examples
javajakarta-eedependency-injectioncdijndi

What's the relation between CDI and JNDI service?


As far as I understand "pre-CDI" EJB and resources injection solutions (@EJB, @Resource and others, to be clear) use the JNDI service to locate objects "known" to the container by virtue of being JNDI registered, and then inject them where requested.

CDI, instead, relies on bean-discovery-mode parameter (ALL or ANNOTATED) to discover beans that need to be managed. But how is this process actually performed? A runtime scan of... what? Is JNDI not involved at all?

I have the feeling I'm getting something wrong about the whole mechanism...


Solution

  • The bean discovery process is described in detail in the CDI specification. Basically, the CDI container scans bean deployment archives for classes with certain annotations.

    JNDI is not involved at all. Unlike EJBs, CDI beans cannot be looked up via JNDI in general.

    Only the BeanManager itself can be looked up under the name of java:comp/BeanManager, but this is almost never required, unless you need to access managed beans from unmanaged code.