title: Bean Injection After DataTable
body: <p>I have a page with a datatable that reads data from a service class.  That service class is suppose to be injected with a contactDAO but it does not get injected right away.  In fact, when the page first loads up, the data table is empty because the DAO has not been injected yet.  However, if I call addContact(), the list 'contacts' is updated fine because the contactDAO is injected by then. </p>
  
  <p>How do I ensure the contactDAO is injected before the datatable needs to use the service class? I am using Spring 3 and JSF 2.0.</p>
  
  <p>The page with the datatable is bind to the list '<code>contactServiceImpl.contacts</code>':</p>
  
  <pre><code>&lt;h:dataTable var="contact" value="#{contactServiceImpl.contacts}"&gt;
  ...
  &lt;/h:dataTable&gt;
  </code></pre>
  
  <p>My ContactServiceImpl looks like this:</p>
  
  <pre><code>@Service
  public class ContactServiceImpl implements ContactService {
  
      private static List&lt;Contact&gt; contacts = new ArrayList&lt;Contact&gt;();
  
      @Autowired
      private static ContactDAO contactDAO;
  
      private ContactServiceImpl() {
          contacts = new ArrayList&lt;Contact&gt;();
  
          //TODO: need to inject contactDAO at the same time as instantiation
          contacts.clear();
          try {
              contacts.addAll( contactDAO.getContacts() );
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void addContact(Contact contact) {
          contacts.add(contact);
          contactDAO.addContact(contact);
  
          contacts.clear();
          contacts.addAll( contactDAO.getContacts() );
      }
  
      @Autowired
      public void setContactDAO(ContactDAO contactDAO) {
          ContactServiceImpl.contactDAO = contactDAO;
          System.out.println("DAO is injected");
      }
  }
  </code></pre>
  
  <p>And applicationContext.xml</p>
  
  <pre><code>  &lt;bean id="contactServiceImpl" class="com.example.service.ContactServiceImpl"
          scope="session"&gt;
          &lt;property name="contactDAO" ref="contactDAOImpl"/&gt;
    &lt;/bean&gt;
  </code></pre>