title: Understanding cdi Instance<> and .get() vs @Inject
body: <p>I'm a little confused about which to use in the following situation:</p>
  
  <p>Suppose the servlet creates an Application that handles the user's http session, and the application is this:</p>
  
  <pre><code>public class Application extends AbstractHTTPApplication {
  
  @Inject
  private Instance&lt;MainView&gt; mainView;
  
  public void setupApplication() {
     this.setView( mainView.get() );
  }
  </code></pre>
  
  <p>Later on I have a <code>@SessionScoped</code> bean <code>SSB</code> that I want to inject into each user's bean:</p>
  
  <pre><code>@SessionScoped
  public class SSB {}
  </code></pre>
  
  <p>Now, when I tried a regular <code>@Inject SSB ssb;</code> as a field in <code>MainView</code>, I do not get a new <code>SSB</code> for each user:</p>
  
  <pre><code>public class MainView {
  
  @Inject
  private SSB usersSSB;
  
     public someMethod() {
         usersSSB.doSomething();
         System.identityHashCode( usersSSB );
     }
  }
  </code></pre>
  
  <p>Testing with two users, I get the <em>same</em> instance of <code>usersSSB</code> in both user's sessions.  I didn't think this was possible... I thought, since SSB is SessionScoped, a new one will be given to each user session, and no matter where it is <code>@Inject</code>ed it will refer to <em>that</em> user's <code>SSB</code>. </p>
  
  <p>Instead, I tried:</p>
  
  <pre><code>public class MainView {
  
  @Inject
  private Instance&lt;SSB&gt; usersSSB;
  
     public someMethod() {
         usersSSB.get().doSomething();
         System.identityHashCode( usersSSB.get() );
     }
  }
  </code></pre>
  
  <p>Now it reports a different <code>usersSSB</code> for each user, finally.</p>
  
  <p>What's happening here? When I call <code>usersSSB.get()</code> later on in each user's session, will the <code>usersSSB.get()</code> return <em>that same bean</em> for <em>that same</em> user each time?</p>
  
  <p>I'm running on Glassfish 3.1.2.</p>
  
  <p><strong>Some More Info</strong></p>
  
  <p>The Application class is being injected into the Servlet on a new HttpServletRequest:</p>
  
  <pre><code>public abstract class AbstractCdiApplicationServlet extends
      AbstractApplicationServlet {
  @Inject
  protected Instance&lt;ApplicationWrapper&gt; wrapper;
  
  @Override
  protected Application getNewApplication(HttpServletRequest request)
          throws ServletException {
      return wrapper.get().getApplication();
  }
  ...etc etc
  </code></pre>
  
  <p>And the <code>ApplicationWrapper</code> is a <code>SessionScoped</code> bean:</p>
  
  <pre><code>@SuppressWarnings("serial")
  @SessionScoped
  public class ApplicationWrapper implements Serializable {
  @Inject
  private AbstractCdiApplication application;
  
  public AbstractCdiApplication getApplication() {
      return application;
  }
   }
  </code></pre>
  
  <p>Doesn't this mean that calling <code>@Inject SSB usersSSB</code> anywhere in MainView (or <em>any</em> object within that user's session) should give me that user's session-scoped bean, and always <em>that same</em> session-scoped bean, for each user's session? Meaning -- different usersSSB for different users, because each user has a different session.</p>
  
  <p>After all, the <code>Application</code> itself is a <code>SessionScoped</code> bean, injected into and attached to the user's HTTP Session by the <code>getNewApplication</code> method of the servlet?  I mean, it is the Application object that injects and attaches the MainView class, after all, right? So that means MainView is a session-scoped bean, doesn't it?</p>
  
  <p>I'm just trying to figure out how this all works, I guess. Thanks for the help!</p>