7 Replies Latest reply on Dec 26, 2007 6:53 PM by alrubinger

    Newbie EJB 3 JNDI question

    deanouk

      Hi all.

      Still confused as to how I should properly use EJB injection within another EJB.

      Say I have an EJB called MyBean. Within it I define the JNDI mapping to

      be: @LocalBinding(jndiBinding="ejb/MyBean/local")


      Within another EJB I grab it using:
      @EJB(name = "ejb/MyBean", beanInterface = MyBeanLocal.class)


      Now currently, I then look it up using
      InitialContext ctx = new InitialContext();
      MyBeanLocal myBeanLocal = (MyBeanLocal ) ctx.lookup("java:comp/env/ejb/MyBean");



      In EJB3, what is the best way of actually injecting a bean into the local JNDI and referencing it, ensuring application server portability?
      I'm still very unsure I'm not going way over the top by ensuring it's injected into the local JNDI from the ENC and then looked up in the local JNDI.

      Any direction would be most helpful, thanks in advance.

        • 1. Re: Newbie EJB 3 JNDI question
          itsme

          On the server side a lookup is'nt needed anymore. Just annotate a member field of the interface type of the necessary ejb like:

          ...
          @EJB private MyBeanLocal bean;
          ...
          

          \sandor\

          • 2. Re: Newbie EJB 3 JNDI question
            deanouk

            Yes, but is this then retrieved from the local ENC?
            How is it in the local ENC?

            • 3. Re: Newbie EJB 3 JNDI question
              gquintana

              I don't (not sure yet) think "java:comp/env" local context is still used with EJB3.

              • 4. Re: Newbie EJB 3 JNDI question
                itsme

                 

                Yes, but is this then retrieved from the local ENC?


                Yes, its the app servers responsibility to resolve the injectected resource from the local enc as long as MyBeanLocal is annotated with @Local (or at least MyBean with @Local({MyBeanLocal.class}).

                Regards
                \sandor\

                • 5. Re: Newbie EJB 3 JNDI question
                  deanouk

                  Ok so I do this, and all I see is a list of global JNDI names in the JMX Console.

                  There are no local references under each of the EJBs to the local ENC EJB references - they're all global!

                  So there *must* be another way of doing this so that we inject into the local ENC?

                  • 6. Re: Newbie EJB 3 JNDI question
                    waynebaylor

                    the jmx-console doesn't display the contents of local ENCs for EJBs. it will display a WAR's ENC... Anyway, it doesn't matter how the app. server injects the bean. entries in the ENC are just "local" names for the EJBs' global JNDI names.

                    • 7. Re: Newbie EJB 3 JNDI question
                      alrubinger

                       

                      "wanebaylor" wrote:
                      the jmx-console doesn't display the contents of local ENCs for EJBs


                      JNDIView bundled with the 5.x series does / will.

                      "gquintana" wrote:
                      I don't (not sure yet) think "java:comp/env" local context is still used with EJB3.


                      When @EJB is defined on EJB A to EJB B, EJB A's local ENC will contain an entry to resolve the dependency. This may be looked up via traditional JNDI (using the java:comp/env namespace, ejbContext.lookup() (without the prefix to the namespace), or via injection.

                      S,
                      ALR