6 Replies Latest reply on May 9, 2008 11:01 AM by Thomas Chang

    A question about dependency injection @EJB

    Thomas Chang Apprentice

      In the EJB3 there is a new feature called dependency injection. With this new feature one doesn't need using the JNDI lookup calls. I try use this but it doesn't work. I get NULL-Pointer Exception.

      Here are my codes:

      @Remote
      public interface MyTestBeanRemote {
       public String test(String msg);
      }
      


      @Stateless
      public class MyTestBeanImpl implements MyTestBeanRemote {
       public String test(String msg) {
       return "Hello, " + msg;
       }
      }
      
      


      public class TestEJB3Client4 {
       @EJB
       private static MyTestBeanRemote beanRemote;
      
       public static void main(String[] args) {
       System.out.println(beanRemote.test("I am here."));
       }
      }
      


        • 1. Re: A question about dependency injection @EJB
          jaikiran pai Master

          In JBoss-4.2.x (or prior versions) the injection will not work for standalone clients or servlets. It works only in the EJB layer. The injection in application clients and servlets is supported in JBoss-5 (which is in Beta).

          • 2. Re: A question about dependency injection @EJB
            Thomas Chang Apprentice

             

            "jaikiran" wrote:
            In JBoss-4.2.x (or prior versions) the injection will not work for standalone clients or servlets. It works only in the EJB layer. The injection in application clients and servlets is supported in JBoss-5 (which is in Beta).

            1.
            I've downloaded the JBoss 5 Beta4. But it's the same, i.e. I got the NULL-Pointer Exception.

            2.
            With JBoss 4.2.1 I can use the
            "@RemoteBinding(jndiBinding = "MyJNDIName")" which you told me yesterday.
            


            But this doesn't work in JBoss 5 Beta4. I can't find the JNDI Name in the JNDI View List. If I run my client, I got Exception like:

            javax.naming.NameNotFoundException: MyJNDIName not bound
            


            • 3. Re: A question about dependency injection @EJB
              jaikiran pai Master

               

              "thomas2008ch" wrote:

              1.
              I've downloaded the JBoss 5 Beta4. But it's the same, i.e. I got the NULL-Pointer Exception.


              I haven't tested it myself. I have seen users mentioning that the injection works in servlets with JBoss-5 Beta 4. But i don't know whether it works in application clients yet.

              "thomas2008ch" wrote:

              2.
              With JBoss 4.2.1 I can use the
              "@RemoteBinding(jndiBinding = "MyJNDIName")" which you told me yesterday.
              


              But this doesn't work in JBoss 5 Beta4. I can't find the JNDI Name in the JNDI View List.


              In JBoss-4.2.x, the @RemoteBinding was part of org.jboss.annotation.ejb package.

              import org.jboss.annotation.ejb.RemoteBinding;


              In JBoss-5, the @Remote binding is now part of org.jboss.ejb3.annotation package.

              import org.jboss.ejb3.annotation.RemoteBinding;


              Did you ensure that you changed your bean to use this new package?




              • 4. Re: A question about dependency injection @EJB
                Thomas Chang Apprentice

                In JBoss-4.2.x, the @RemoteBinding was part of org.jboss.annotation.ejb package.

                import org.jboss.annotation.ejb.RemoteBinding;


                In JBoss-5, the @Remote binding is now part of org.jboss.ejb3.annotation package.

                import org.jboss.ejb3.annotation.RemoteBinding;


                Did you ensure that you changed your bean to use this new package?



                • 5. Re: A question about dependency injection @EJB
                  Thomas Chang Apprentice

                  But this doesn't work in JBoss 5 Beta4. I can't find the JNDI Name in the JNDI View List.

                  In JBoss-4.2.x, the @RemoteBinding was part of org.jboss.annotation.ejb package.

                  import org.jboss.annotation.ejb.RemoteBinding;


                  In JBoss-5, the @Remote binding is now part of org.jboss.ejb3.annotation package.

                  import org.jboss.ejb3.annotation.RemoteBinding;


                  • 6. Re: A question about dependency injection @EJB
                    Thomas Chang Apprentice

                    To jaikiran:

                    You are right.

                    Many thanks!