将bean 1注入bean 2:bean 1的状态变化是否会反映在bean 2中?

时间:2014-09-07 09:35:12

标签: jsf cdi jsf-2.2

GlassFish4,JSF2.2,JavaEE7:

ManagedBean controllerBean被注入ManagedBean pb(for progressBar):

@ManagedBean
@ViewScoped
public class Pb implements Serializable{

    public Pb() {
    }

    @ManagedProperty(value = "#{controllerBean}")
    ControllerBean controllerBean;

    public void setControllerBean(ControllerBean controllerBean) {
        this.controllerBean = controllerBean;
    }

    public Integer getProgress() {
        return controllerBean.getNumberOfComputations();
    }
}

- >方法getProgress()numberOfcomputations为增量返回不同的值作为整数controllerBean

但是当我使用@Inject代替@ManagedProperty时:

@ManagedBean
@ViewScoped
public class Pb implements Serializable{

    public Pb() {
    }

    @Inject
    @ControllerBean controllerBean;


    public Integer getProgress() {
        return controllerBean.getNumberOfComputations();
    }
}

然后getProgress()一直返回0,这是controllerBean中numberOfComputations的初始状态,即使此整数在controllerBean中快速增加。

发生了什么事?

1 个答案:

答案 0 :(得分:1)

您应该用CDI替换所有JSF注释:

@ManagedBean => @Named

@javax.faces.bean.ViewScoped => @javax.faces.view.ViewScoped

@ManagedProperty => @Inject