3 Replies Latest reply on Oct 6, 2011 6:54 AM by bankai

    Seam component not being injected

    bankai

      I have an element 'Contact' i want injected in my RegisterContact class, but the injection isn't working. The component is 'null'.


      From the following code snippet, you can see I've called Component.getInstance, which obviously isn't injection, but it's shown that my component is in context and accessible with data.
      Can someone provide some guidance please....


      I am only just starting off with Seam, so I may be making some rookie mistakes, I just don't know where to look.


      @Stateless
      @Name("register")
      
      public class RegisterContact implements Register{
      
              @In
              private Contact contact;
      
              @PersistenceContext
              private EntityManager em;
      
              @Logger
              private Log log;
      
              public String register(){
      
                      contact = (Contact) Component.getInstance("contact");
      
                      List<Contact> existing;
      
                      existing = em.createQuery("select firstname from Contact where firstname=:firstname")
                      .setParameter("firstname",contact.getFirstname())
                      .getResultList();
      
                      if ( existing.size() == 0 ) {
                              em.persist(contact);
                              return "/registered.xhtml";
                      }
      
                      FacesMessages.instance().add("A user with the name #{contact.firstname} already exists");
                      return null;
      
              }
      
      }




        • 1. Re: Seam component not being injected
          cosmo

          @In does not create the component by default, it just inject what is in a specific context. If you need to create the component at injection time you can do it either by specifying create=true in the @In, or annotating the injected component with @Autocreate

          • 2. Re: Seam component not being injected
            bankai

            The component is in context, I can see it in JBoss debug and if it weren't the following call would fail, wouldn't it?



               contact = (Contact) Component.getInstance("contact");
            



            As for the create=true option, I don't want seam to create a new Contact component if it can't find one in context, in this case.

            • 3. Re: Seam component not being injected
              bankai

              Seems I was missing the ejb-jar.xml file





              "<?xml version="1.0" encoding="UTF-8"?>
              <ejb-jar 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/ejb-jar_3_0.xsd"
                       version="3.0">
                       
                 <interceptors>
                   <interceptor>
                     <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
                   </interceptor>
                 </interceptors>
                 
                 <assembly-descriptor>
                    <interceptor-binding>
                       <ejb-name>*</ejb-name>
                       <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
                    </interceptor-binding>
                 </assembly-descriptor>
                 
              </ejb-jar>"