title: @ApplicationScoped beans get constructed more than once
body: <p>I have two managed Java beans:</p>
  
  <pre><code>import javax.annotation.PostConstruct;
  import javax.faces.bean.ApplicationScoped;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ManagedProperty;
  
  import javax.ws.rs.Path;
  
  @Path("/sync")
  @ManagedBean(name="syncService", eager=true)
  @ApplicationScoped
  public class SyncService {
      @ManagedProperty(value="#{ldapDirectoryAccess}")
      private DirectoryAccess directoryAccess;
  
      public void setDirectoryAccess(DirectoryAccess directoryAccess) {
          System.out.println("SyncService.setDirectoryAccess()");
          this.directoryAccess = directoryAccess;
      }
  
      public SyncService() {
          System.out.println("SyncService() - constructed: " + this);
      }
  
      @PostConstruct
      public void init() {
          System.out.println("DirectoryAccess injected: " + directoryAccess + " in: " + this);
      }
      ...
  }
  
  @ManagedBean(name="ldapDirectoryAccess", eager=true)
  @ApplicationScoped
  public class LdapDirectoryAccess implements DirectoryAccess {
      public LdapDirectoryAccess() {
          System.out.println("LdapDirectoryAccess constructed: " + this);
      }
      ...
  }
  </code></pre>
  
  <p>When I deploy the application in Tomcat, I get the following output in <code>catalina.out</code>:</p>
  
  <pre><code>SyncService() - constructed: ...SyncService@705ebb4d
  ...
  LdapDirectoryAccess constructed: ...LdapDirectoryAccess@3c1fd5aa
  SyncService.setDirectoryAccess()
  DirectoryAccess injected: ...LdapDirectoryAccess@3c1fd5aa in:
                            ...SyncService@705ebb4d
  LdapDirectoryAccess constructed: ...LdapDirectoryAccess@59d6a4d1
  </code></pre>
  
  <p>So, first an instance of each bean is constructed as expected, and the second bean is injected into the first. But then, another instance of the second bean class is created. How is this possible? In <a href="http://www.tutorialspoint.com/jsf/jsf_managed_beans" rel="nofollow noreferrer">this tutorial</a> I found the following:</p>
  
  <blockquote>
    <p>@ApplicationScoped</p>
    
    <p>Bean lives as long as the web application lives. It gets created upon
    the first HTTP request involving this bean in the application (or when
    the web application starts up and the eager=true attribute is set in
    @ManagedBean) and gets destroyed when the web application shuts down.</p>
  </blockquote>
  
  <p>So I would expected that one instance of each bean is created when the application is started, and that both instances are destroyed when the application is shut down. But <code>LdapDirectoryAccess</code> is constructed twice.</p>
  
  <p>Moreover, when I open the page that is served by <code>SyncService</code> I see:</p>
  
  <pre><code>SyncService() - constructed: ... SyncService@1cb4a09c
  </code></pre>
  
  <p>so a second instance of <code>SyncService</code> is built as well, and I cannot understand why. Also, no <code>directoryAccess</code> property is injected this time, and the service throws a null pointer exception.</p>
  
  <p>This means that the first instance of <code>SyncService</code> is built correctly, but then</p>
  
  <ol>
  <li>A second instance of <code>SyncService</code> is created (why?)</li>
  <li>No <code>LdapDirectoryAccess</code> is injected into it (why?)</li>
  <li>This second instance of <code>SyncService</code> is used to serve the call to my REST API. Why this instance and not the first one that was created?</li>
  </ol>
  
  <p>I have looked at <a href="https://stackoverflow.com/questions/13007291/eager-applicationscoped-managed-beans-constructed-multiple-times">this question</a> and its answers, however:</p>
  
  <ul>
  <li>I am using Mojarra 2.2.18</li>
  <li>My application's <code>web.xml</code> does not contain any tag mentioning <code>com.sun.faces.config.ConfigureListener</code></li>
  </ul>
  
  <p>So I after several hours investigation I am completely out of ideas. Do you have any hints?</p>