CDI SessionScoped Bean在同一会话中产生两个实例

Rya*_*yan 5 jsf cdi

我有两个SessionScoped CDI bean实例用于同一个会话.我的印象是CDI会为我生成一个实例,但它产生了两个.我误解了CDI是如何工作的,还是我发现了一个错误?

这是bean代码:

package org.mycompany.myproject.session;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

@Named @SessionScoped public class MyBean implements Serializable {
    private String myField = null;

    public MyBean() {
        System.out.println("MyBean constructor called");

        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession)fc.getExternalContext().getSession(false);
        String sessionId = session.getId();
        System.out.println("Session ID: " + sessionId);
    }

    public String getMyField() {
        return myField;
    }

    public void setMyField(String myField) {
        this.myField = myField;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是Facelet代码:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<f:view contentType="text/html" encoding="UTF-8">
    <h:head>
        <title>Test</title>
    </h:head>
    <h:body>
        <h:form id="form">
            <h:inputText value="#{myBean.myField}"/>
            <h:commandButton value="Submit"/>
        </h:form>
    </h:body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)

以下是部署和导航到页面的输出:

INFO: Loading application org.mycompany_myproject_war_1.0-SNAPSHOT at /myproject
INFO: org.mycompany_myproject_war_1.0-SNAPSHOT was successfully deployed in 8,237 milliseconds.
INFO: MyBean constructor called
INFO: Session ID: 175355b0e10fe1d0778238bf4634
INFO: MyBean constructor called
INFO: Session ID: 175355b0e10fe1d0778238bf4634
Run Code Online (Sandbox Code Playgroud)

使用GlassFish 3.0.1

str*_*erg 6

Ryan,正如 covener 已经写的那样,构造函数也将被调用为该 bean 的每个代理。这是所有代理机制的标准行为,它不仅提供接口代理(如 java.lang.reflect.proxy 的东西)而且提供真正的类代理。

还想象一下,ct 也将被每个序列化调用。因此,如果您在负载均衡的集群上工作,您会多次看到这种情况。所以请在一般情况下对 bean 使用 @PostConstruct。

LieGrue,stub

  • Ryan,我知道所有代理的东西都不是那么容易,但是获得 2 个构造函数调用是完全没问题的。第一个实例是上下文实例本身。这是您将存储在 SessionContext 中的 bean。第二个实例是代理。如果您仔细查看调试器或在构造函数中打印出类,那么您将看到这实际上是您的 bean 的子类! (2认同)