title: how is the @RequestScoped bean instance provided to @SessionScoped bean in runtime here?
body: <p>I am reading through this example in <a href="http://docs.jboss.org/weld/reference/latest/en-US/html/example.html" rel="nofollow">JBoss</a> where a <code>@RequestScoped</code> bean backing up <code>JSF page</code> is used to pass the user credential information which is then saved in a <code>@sessionScoped bean</code>.
  Here is the example take from <a href="http://docs.jboss.org/weld/reference/latest/en-US/html/example.html" rel="nofollow">JBoss docs.</a> </p>
  
  <pre><code>@Named @RequestScoped
  public class Credentials {
      private String username;
      private String password;
      @NotNull @Length(min=3, max=25)
      public String getUsername() { return username; }
      public void setUsername(String username) { this.username = username; }
      @NotNull @Length(min=6, max=20)
      public String getPassword() { return password; }
      public void setPassword(String password) { this.password = password; }
  }
  </code></pre>
  
  <p>JSF form:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;h:form&gt;
     &lt;h:panelGrid columns="2" rendered="#{!login.loggedIn}"&gt;
        &lt;f:validateBean&gt;
           &lt;h:outputLabel for="username"&gt;Username:&lt;/h:outputLabel&gt;
           &lt;h:inputText id="username" value="#{credentials.username}"/&gt;
           &lt;h:outputLabel for="password"&gt;Password:&lt;/h:outputLabel&gt;
           &lt;h:inputSecret id="password" value="#{credentials.password}"/&gt;
        &lt;/f:validateBean&gt;
     &lt;/h:panelGrid&gt;
     &lt;h:commandButton value="Login" action="#{login.login}" rendered="#{!login.loggedIn}"/&gt;
     &lt;h:commandButton value="Logout" action="#{login.logout}" rendered="#{login.loggedIn}"/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>User Entity:</p>
  
  <pre><code>@Entity
  public class User {
     private @NotNull @Length(min=3, max=25) @Id String username;
     private @NotNull @Length(min=6, max=20) String password;
     public String getUsername() { return username; }
     public void setUsername(String username) { this.username = username; }
     public String setPassword(String password) { this.password = password; }
  }
  </code></pre>
  
  <p>SessionScoped bean</p>
  
  <pre><code>@SessionScoped @Named
      public class Login implements Serializable {
  
     @Inject Credentials credentials;
     @Inject @UserDatabase EntityManager userDatabase;
     private User user;
     public void login() {
        List&lt;User&gt; results = userDatabase.createQuery(
           "select u from User u where u.username = :username and u.password = :password")
           .setParameter("username", credentials.getUsername())
           .setParameter("password", credentials.getPassword())
           .getResultList();
        if (!results.isEmpty()) {
           user = results.get(0);
        }
        else {
           // perhaps add code here to report a failed login
        }
     }
  
     public void logout() {
        user = null;
     }
  
     public boolean isLoggedIn() {
        return user != null;
     }
  
     @Produces @LoggedIn User getCurrentUser() {
   return user;
     }
  }
  </code></pre>
  
  <p>My questions are</p>
  
  <p>1) The <code>@RequestScoped</code> bean gets injected into <code>@SessionScoped</code> bean. What is the guarantee that the credential information set on one instance of <code>RequestScoped</code> is the same that is injected into <code>@SessionScoped</code>bean. why not a different <code>@RequestScoped</code> from pool gets injected or even a new instance?</p>
  
  <p>2)why is the bean given <code>@SessionScoped</code> but not <code>@Stateful</code>. I guess <code>@Stateful</code> will work here.</p>
  
  <p>3)how is the lifecycle of <code>@sessionScoped</code> bean managed? That is when does it gets destroyed ?. If I navigate to a different <code>JSF</code> page in which if I pull the information  such as <code>currentUser.userName</code>, will I retrieve the same information I set on my first <code>JSF</code> page used to log in. (step 1 above)</p>
  
  <p>4) If I don't specify <code>@RequestScoped</code>, then the Credentials bean get the <code>@Dependent</code> scope which is the defualt scope. It is mentioned in the <a href="http://docs.jboss.org/weld/reference/latest/en-US/html/scopescontexts.html#_the_dependent_pseudo_scope" rel="nofollow">docs</a> that setting any instance variables of a <code>@Dependent</code> gets lost immediately. But I don't understand why? In fact, this prompts me the question of what use of <code>@Dependent</code> scope will be ?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong>
  Thanks kolossus for detailed and excellent answer. I need a little more clarifications on some of your points for better understanding</p>
  
  <ol>
  <li>For a @requestScoped bean, there are is a pool of instances available which gets handed over to clients. Now if I have two clients accessing a JSF which is backed by a <code>@RequestScoped</code> bean, each client gets to work on one instance of <code>@RequestScoped</code> bean from the pool. In fact, both the clients do not actually work on the direct instance, but an indirect reference to the that single instance which is the proxy here. clients do all method calls or transactions using this proxy. so how long does the proxy holds this indirect reference? That is, in my example above, instance variables of <code>@RequestScoped</code> bean (<code>Credentials</code>) are set in JSF. but the true fact is that, this setting of instance variables happen to one instance of @RequestScoped bean indirectly through proxy. But when this instance is injected into <code>SessionScoped</code> bean, is it the proxy that gets injected? Since the lifecycle of <code>SessionScoped</code> is for a session established between client and application, does the proxy also live during this lifetime. Does that mean this <code>single instance of @RequestScoped bean</code> is bound to <code>SessionScoped</code> and the lifecycle of <code>@RequestScoped</code> bean instance or its proxy is determined by the lifecycle of <code>SessionScoped</code> bean?</li>
  </ol>