jsf - @RequestScoped bean 实例是如何在运行时提供给 @SessionScoped bean 的?

标签 jsf jakarta-ee ejb cdi managed-bean

我正在阅读 JBoss 中的这个例子在哪里 @RequestScoped bean 备份JSF page用于传递用户凭据信息,然后将其保存在 @sessionScoped bean 中.
这是来自 JBoss docs. 的示例

@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; }
}

JSF 形式:

<h:form>
   <h:panelGrid columns="2" rendered="#{!login.loggedIn}">
      <f:validateBean>
         <h:outputLabel for="username">Username:</h:outputLabel>
         <h:inputText id="username" value="#{credentials.username}"/>
         <h:outputLabel for="password">Password:</h:outputLabel>
         <h:inputSecret id="password" value="#{credentials.password}"/>
      </f:validateBean>
   </h:panelGrid>
   <h:commandButton value="Login" action="#{login.login}" rendered="#{!login.loggedIn}"/>
   <h:commandButton value="Logout" action="#{login.logout}" rendered="#{login.loggedIn}"/>
</h:form>

用户实体:
@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; }
}

SessionScoped bean
@SessionScoped @Named
    public class Login implements Serializable {

   @Inject Credentials credentials;
   @Inject @UserDatabase EntityManager userDatabase;
   private User user;
   public void login() {
      List<User> 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;
   }
}

我的问题是

1) @RequestScoped bean 被注入(inject) @SessionScoped bean 。 RequestScoped 的一个实例上设置的凭证信息有什么保证?与注入(inject) @SessionScoped 的相同 bean 。为什么不换一个 @RequestScoped from pool 被注入(inject)甚至是一个新实例?

2)为什么给出@SessionScoped的bean但不是 @Stateful .我猜@Stateful将在这里工作。

3)@sessionScoped 的生命周期如何? bean 管理?那是它什么时候被摧毁?如果我导航到不同的 JSF如果我在其中提取信息,例如 currentUser.userName ,我会检索到我在第一个 JSF 中设置的相同信息吗?用于登录的页面。(上面的第 1 步)

4) 如果我不指定 @RequestScoped ,然后 Credentials bean 得到 @Dependent范围是默认范围。 docs中提到了设置 @Dependent 的任何实例变量立即迷路。但我不明白为什么?事实上,这让我想到了 @Dependent 有什么用?范围将是?

谢谢

编辑
感谢 kolossus 提供详细而出色的答案。我需要对您的一些观点进行更多澄清,以便更好地理解
  • 对于@requestScoped bean,有一个可用的实例池可以移交给客户端。现在,如果我有两个客户端访问由 @RequestScoped 支持的 JSF bean,每个客户端都可以处理 @RequestScoped 的一个实例池子里的 bean 。事实上,这两个客户端实际上并不在直接实例上工作,而是对作为代理的单个实例的间接引用。客户端使用此代理执行所有方法调用或事务。那么代理持有这个间接引用多久呢?也就是说,在我上面的示例中,@RequestScoped 的实例变量bean ( Credentials ) 在 JSF 中设置。但真正的事实是,实例变量的这种设置通过代理间接发生在@RequestScoped bean 的一个实例上。但是当这个实例被注入(inject) SessionScoped bean,是被注入(inject)的代理吗?由于 SessionScoped 的生命周期对于客户端和应用程序之间建立的 session ,代理是否也在此生命周期内存在。这是否意味着 single instance of @RequestScoped bean绑定(bind)到 SessionScoped@RequestScoped 的生命周期bean 实例或其代理由 SessionScoped 的生命周期决定 bean ?
  • 最佳答案

  • The @RequestScoped bean gets injected into @SessionScoped bean. What is the guarantee that the credential information set on one instance of RequestScoped is the same that is injected into @SessionScopedbean. why not a different @RequestScoped from pool gets injected or even a new instance?



    这是合法的,这要归功于 CDI 实际获取对所请求 bean 的引用的方式:client proxies .来自 the CDI spec

    An injected reference, or reference obtained by programmatic lookup, is usually a contextual reference.A contextual reference to a bean with a normal scope[...], is not a direct reference to a contextual instance of the bean[...].Instead, the contextual reference is a client proxy object A client proxy implements/extends some or all of the bean types of the bean and delegates all method calls to the current instance of the bean...



    这种间接的原因有很多:


  • 容器必须保证当调用任何对正常范围的 bean 的有效注入(inject)引用时,调用总是由注入(inject) bean 的当前实例处理。在某些情况下,例如,如果将请求范围的 bean 注入(inject)到 session 范围的 bean 或 servlet 中,则此规则需要间接引用


  • 也来自 this DZone CDI article :

    CDI handles the injection of beans with mismatched scopes through the use of proxies. Because of this you can inject a request scoped bean into a session scoped bean and the reference will still be valid on each request because for each request, the proxy re-connects to a live instance of the request scoped bean



    这意味着,在每个注入(inject)点都用代理代替了真实的东西。代理通过扩展/实现它应该模仿的类型的祖先树来模仿在注入(inject)点声明的类型。在您现在实际需要使用该对象时,代理会在当前对话中对请求的 bean 的现有实例执行基于上下文的查找。这是一个请求范围的对象,您可以保证在当前对话/上下文中只有一个实例。
  • why is the bean given @SessionScoped but not @Stateful. I guess @Stateful will work here.


    @Stateful不会在这里工作,就像我说的 here ,它们并不便宜;除非你真的需要,否则坚持原味 HttpSession .更不用说一旦 SFSB 的客户端释放 bean,它就会被销毁,即 SFSB 不绑定(bind)到当前 session ,@SessionScoped是。
  • how is the lifecycle of @sessionScoped bean managed? That is when does it gets destroyed ?. If I navigate to a different JSF page in which if I pull the information such as currentUser.userName, will I retrieve the same information I set on my first JSF page used to log in. (step 1 above)



    取决于哪个 @SessionScoped您指的是:javax.faces.bean.SessionScoped直接绑定(bind)到当前 HttpSession/browser session ,所以它在任何时候终止; JBoss 然而implies that javax.enterprise.context.* scoped beans don't actually go anywhere until the "context" dies

    There's actually no way to remove a bean from a context until the entire context is destroyed

  • 想想@Dependent就像任何方法局部变量一样:它只有在它的父构造存在时才有用。话虽如此,最好的用途不是支持 JSF View 。它最有用的应用程序是覆盖在 bean 上指定的范围,即席。使用您当前的示例,我可以在我的应用程序的其他地方拥有以下内容:
    @Inject @New Login aDependentLoginBean; //implicit @Dependent scope applied
    @Inject Login aSessionScopedLoginBean;  //standard Login bean's scope applied
    

    连同@New ,您可以将任何其他 bean 重新用作 @Dependent


  • 相关:
  • Is it possible to @Inject a @RequestScoped bean into a @Stateless EJB?
  • 关于jsf - @RequestScoped bean 实例是如何在运行时提供给 @SessionScoped bean 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26305295/

    相关文章:

    jsf - Primefaces 可编辑数据表中是否有行选择事件?

    java - Servlet LoginController 的 Servlet.service() 抛出异常 java.lang.ClassNotFoundException : Decoder. BASE64Encoder

    java - EJB SessionBean - @TransactionManagement 和@TransactionAttribute 的组合

    java - 差异 : @SessionScoped vs @Stateful and @ApplicationScoped vs @Singleton

    java - Servlet 和后端关系

    jakarta-ee - 避免容器管理的 EntityManager 超时

    jsf - 删除网址中的jsessionid

    image - JSF 2.0 应用程序中的本地化图像

    ajax - jsf ajax部分渲染

    java - Grails 是大型企业 Web 应用程序的可行选择吗?