9 Replies Latest reply on Oct 31, 2009 5:51 PM by Henk Haveman

    EJB injection returns null

    Henk Haveman Newbie

      Hello,

      I am trying to use inject a stateless bean in a managed bean in a jsf web application.
      However the EJB bean is null. If I try to do the same thing using jndi lookup the result is also an NULL object.

      If I try to lookup the bean in a client application everything works fine.

      In am using Jboss AS 5.1.0.GA

      The code of the managed bean is :

      package mx.bean;
      
      import java.util.List;
      
      import javax.ejb.EJB;
      import javax.naming.Binding;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      
      import mx.business.land.LandLocal;
      import mx.business.land.LandRemote;
      import mx.model.Land;
      
      public class LandBean {
      
       private static final Log LOG = LogFactory.getLog(LandBean.class);
      
       @EJB(name = "ear/LandBean/remote", mappedName = "ear/LandBean/remote")
       private LandRemote landHandler;
      
       private List<Land> landen;
      
       public LandBean() throws NamingException {
       LOG.info("LandBean");
       if (landen == null) {
       LOG.info("LandBean landen is null");
       }
       Context context = new InitialContext();
       landHandler = (LandRemote) context.lookup("ear/LandBean/remote");
       if (landen == null) {
       LOG.info("LandBean landen is nog steeds null");
       }
       }
      
      
       /**
       * @param landen the landen to set
       */
       public void setLanden(List<Land> landen) {
       this.landen = landen;
       }
      
       /**
       * @return the landen
       */
       public List<Land> getLanden() {
       if (landen == null) {
       LOG.info("Landen is null");
       } else {
       landen = landHandler.getAll();
       }
       return landen;
       }
      
      
      
      }
      


      The logging shows :


      22:55:57,757 INFO [LandBean] LandBean
      22:55:57,757 INFO [LandBean] LandBean landen is null
      22:55:57,776 INFO [LandBean] LandBean landen is nog steeds null
      22:55:57,779 INFO [LandBean] Landen is null

      Could anybody tell me what I am doing wrong? Or is injection not possible in a JSF web application?

      Thanks,


      Henk




        • 1. Re: EJB injection returns null
          Jim Johnson Newbie

          Shouldn't

          if (landen == null) {
           LOG.info("LandBean landen is null");
           }
          


          be

          if (landHandler == null) {
           LOG.info("LandBean landen is null");
           }
          


          Not sure if it's just a transcription error but your code posted to the forum is checking if the uninitialized List is null.

          That said, I'm not sure if Injection works into a managed bean.

          • 2. Re: EJB injection returns null
            Henk Haveman Newbie

            Thanks,

            that did the trick. That's code completion in an IDE for you, without looking closely which variable to pick. I can now access the session bean using jndi lookup, however injecting the bean using the @EJB annotaion still results in a NULL object. But at least I can use the bean now.

            Henk

            • 3. Re: EJB injection returns null
              John Ament Master

              LandBean itself is not a managed object. @EJB injection only works on managed objects, JSF components, servlets, and EJBs.

              • 4. Re: EJB injection returns null
                John Ament Master

                ah sorry i read it wrong.

                can you provide some of the configuration on the Remote? and the implementation? either deployment desc or annotations?

                • 5. Re: EJB injection returns null
                  Henk Haveman Newbie

                  The injected bean is a stateless session bean. This bean is deployed as a part of an ear.
                  When I perform a jndi lookup in jmx-console I can see it is there. Also when deploying the ear
                  I can see messages of the bean being added to jndi.

                  00:15:52,472 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
                  
                   ear/LandBean/remote - EJB3.x Default Remote Business Interface
                   ear/LandBean/remote-mx.business.land.LandRemote - EJB3.x Remote Business Interface
                   ear/LandBean/local - EJB3.x Default Local Business Interface
                   ear/LandBean/local-mx.business.land.LandLocal - EJB3.x Local Business Interface
                  
                  


                  The code of the bean is

                  
                  package mx.business.land;
                  
                  import java.util.List;
                  
                  import javax.ejb.Stateless;
                  import javax.persistence.EntityManager;
                  import javax.persistence.PersistenceContext;
                  import javax.persistence.Query;
                  
                  import mx.model.Land;
                  
                  @Stateless
                  public class LandBean implements LandLocal, LandRemote {
                  
                   @PersistenceContext(unitName = "mx")
                   private EntityManager em;
                  
                   @SuppressWarnings("unchecked")
                   public List<Land> getAll() {
                   Query query = em.createQuery("from Land");
                   List<Land> rtrn = query.getResultList();
                   return rtrn;
                   }
                  
                   public Land getById(String pCode) {
                   Land rtrn = em.find(Land.class, pCode);
                   return rtrn;
                   }
                  
                  }
                  


                  Everything works fine if I do a jndi lookup, but using injection with the @EJB annotation gives me a null object. In my code I now check if the injection succeeded. If not I use jndi lookup. This works for me, but it would be much nicer if injection always succeeded.

                  Henk


                  • 6. Re: EJB injection returns null
                    John Ament Master

                    unless you have an ejb-jar that's overriding it, remove the name attribute in your client.

                    e.g.

                    @EJB(mappedName = "ear/LandBean/remote")
                    private LandRemote landHandler;
                    


                    • 7. Re: EJB injection returns null
                      Henk Haveman Newbie

                      I tried it, but it makes no difference. The bean is still null.

                      • 8. Re: EJB injection returns null
                        jaikiran pai Master

                        You probably are not using the 2.5 xsd version in your web.xml. You need this:

                        <web-app version="2.5"
                         xmlns="http://java.sun.com/xml/ns/javaee"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
                        


                        • 9. Re: EJB injection returns null
                          Henk Haveman Newbie

                          I am using the 2.5 xsd in my web.xml, so that's not causing it.