4 Replies Latest reply on Mar 27, 2013 4:12 PM by nickarls

    @Inject session bean into war

    klind

      JBoss 7.1.1-final

       

      I have a Simple session bean in the EJB module, and inject this session bean in a class in the war module.

       

      But I get a NullPointerException when runnig the code... injection is not working..

      I got a setup that is very similar to this where it works, and I can't seem to find any differences on how the injection is done..

       

      I do not use the injected sessionbean in the constructor.

       

       

      @Stateless(name = "HibernateUtilsSBBean", mappedName = "jsi/ejb/HibernateUtilsSBBean")
      public class HibernateUtilsSBBean implements HibernateUtilsSBBeanLocal, HibernateUtilsSBBeanRemote {
           ......
      }
      

       

       

      @Local
      public interface HibernateUtilsSBBeanLocal {
      ....
      }
      

       

       

      @Remote
      public interface HibernateUtilsSBBeanRemote {
      ...
      }
      

       

       

      pojo class in the web app that @Inject sessionbean. 
      
      @Inject
          private HibernateUtilsSBBeanLocal hibernateUtils;
      
      

       

       

      Maven project.. 
      EAR
           - WAR
                - WEB-INF
                     beans.xml
                     classes
                          com.foo.... class that @Inject sesison bean interface
           - EJB
                - META-INF
                     beans.xml
           - META-INF
                beans.xml
                com.foo.... session bean class
           lib
               jar-containing-interfaces 
      
        • 1. Re: @Inject session bean into war
          nickarls

          That's a bit strange since usually the CDI implementation complains about injections points it can't fulfill. The only case where this usually happens when the instance doesn't come from the container (e.g. you have used new()), in which case injection can't occur.

          • 2. Re: @Inject session bean into war
            sfcoy

            How does the object of the "pojo class in the web app that @Inject sessionbean" get created?

             

            Injection only happens if the container creates the object for you.

            1 of 1 people found this helpful
            • 3. Re: @Inject session bean into war
              klind

              This is an existing project where I slowly wanna implement injections, and you guys are right.

              The pojo is created with the new operator.

               

              So I guess I have to use @injection all the way... starting from the servlet ??

              • 4. Re: @Inject session bean into war
                nickarls

                Yep, @Inject (or @EJB) is the way to go.