CDI bean构造函数和@PostConstruct多次调用

问题描述:

经过一些试验,这似乎是使用CDI bean时出现的问题,与我最初假定的与PrimeFaces无关.

After some experimentation, this appears to be an issue when using a CDI bean, and nothing to do with PrimeFaces which is what I originally assumed.

如果我使用@ManagedBean将TableBeanLazy更改为Managed Bean,则构造函数和@PostConstruct方法仅被调用一次,正如我所期望的那样.

If I change the TableBeanLazy to be a Managed Bean using @ManagedBean, then the constructor and @PostConstruct method only get called once, as I was expecting.

任何人都可以阐明这一点吗?

Can anyone shed any light on this?

================================================ ====

===================================================

我对JSF和PrimeFaces还是很陌生,因此任何指导将不胜感激.我搜索了其他帖子,并阅读了许多类似的帖子,但是找不到解决方法.

I'm quite new to JSF and PrimeFaces, so any guidance will be greatly appreciated. I've searched for other postings, and read a number of similar ones, but can't find a solution.

我正在尝试在我的项目中使用的PrimeFaces Showcase网站上获取延迟加载数据表"示例,但是由于bean类的行为异常,我遇到了问题.

I am trying to get the Lazy Loading Data Table example on the PrimeFaces Showcase site working in my project, but I'm having problems due to the bean class not behaving as I was expecting.

我已经从PrimeFaces站点对示例代码进行了一些更改:-将TableBean类重命名为TableBeanLazy;在类中添加了注释;分配静态字符串数组;将初始化从构造函数移至@PostConstruct initialise()方法;分配了 cars 成员变量.

I've made a few changes to the sample code from the PrimeFaces site:- renamed TableBean class to TableBeanLazy; added annotations to the class; allocated static String arrays; moved initialisation from the constructor to the @PostConstruct initialise() method; allocated the cars member variable.

我还向构造函数和@PostConstruct方法中添加了跟踪消息,这些消息显示在第一次显示表时多次调用这两者,每次单击搜索图标都多次调用,并且每个字符多次过滤器字段中输入.

I've also added trace messages to the constructor and the @PostConstruct method which show that both are being called multiple times when the table is first displayed, multiple times with each click of a search icon, and multiple times with each character entry in a filter field.

这将导致每次执行操作时都会重新创建Car对象的随机列表,因此排序和过滤将永远不会产生预期的结果-当直接在PrimeFaces Showcase网站上运行示例时,它们就会这样做.

This results in the random list of Car objects being recreated each time an action is taken, so sort and filter will never produce the expected results - which they do when running the example directly on the PrimeFaces Showcase site.

尽管我最初使用的是@ViewScoped,但我也尝试了@RequestScoped,最后尝试了@SessionScoped,但是它们的行为均相同.

Although I was originally using @ViewScoped, I have also tried @RequestScoped and finally @SessionScoped, but all behave in the same way.

多次调用构造函数和@PostConstruct是bean的预期行为吗?

Is multiple calls to the constructor and @PostConstruct the expected behaviour for a bean?

如果是这样,PrimeFaces Showcase网站上给出的示例代码如何工作?

If so, how can the sample code given on the PrimeFaces Showcase site possibly work?

我该如何解决?

环境

PrimeFaces 3.5
JSF 2.1
JDK 1.7
GlassFish Server Open Source Edition 3.1.2.2 (build 5)
Mojarra 2.1.6 (SNAPSHOT 20111206)
Netbeans IDE 7.3
Windows 7 Pro x64

MyTable.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  template="./resources/templates/platform_tpl.xhtml"
  xmlns:p="http://primefaces.org/ui"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">

  <ui:define name="body">
    <h2 style="margin-top: 0px; padding-top: 0px;">Cars List</h2>

    <h:form id="form">
      <p:dataTable var="car" value="#{tableBeanLazy.lazyModel}" paginator="true" rows="10"
        paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
        rowsPerPageTemplate="5,10,15" selectionMode="single" selection="#{tableBeanLazy.selectedCar}" id="carTable" lazy="true">

      <p:column headerText="Model" sortBy="#{car.model}" filterBy="#{car.model}">
        <h:outputText value="#{car.model}" />
      </p:column>

      <p:column headerText="Year" sortBy="#{car.year}" filterBy="#{car.year}">
        <h:outputText value="#{car.year}" />
      </p:column>

      <p:column headerText="Manufacturer" sortBy="#{car.manufacturer}" filterBy="#{car.manufacturer}">
        <h:outputText value="#{car.manufacturer}" />
      </p:column>

      <p:column headerText="Color" sortBy="#{car.color}" filterBy="#{car.color}">
        <h:outputText value="#{car.color}" />
      </p:column>
      </p:dataTable>
    </h:form> 
  </ui:define>
</ui:composition>

TableBeanLazy.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BackEnd;

import java.util.ArrayList;
import java.util.List;  
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;  

@Named(value = "tableBeanLazy")
@SessionScoped
public class TableBeanLazy
{
  static String[] colors = new String[10];
  static String[] manufacturers = new String[10];
  static String[] models = new String[5];

  private LazyDataModel<Car> lazyModel;  

  private Car selectedCar;  

  private List<Car> cars;  

  static
  {
    colors[0] = "Black";  
    colors[1] = "White";  
    colors[2] = "Green";  
    colors[3] = "Red";  
    colors[4] = "Blue";  
    colors[5] = "Orange";  
    colors[6] = "Silver";  
    colors[7] = "Yellow";  
    colors[8] = "Brown";  
    colors[9] = "Maroon";  

    manufacturers[0] = "Mercedes";  
    manufacturers[1] = "BMW";  
    manufacturers[2] = "Volvo";  
    manufacturers[3] = "Audi";  
    manufacturers[4] = "Renault";  
    manufacturers[5] = "Opel";  
    manufacturers[6] = "Volkswagen";  
    manufacturers[7] = "Chrysler";  
    manufacturers[8] = "Ferrari";  
    manufacturers[9] = "Ford";

    models[0] = "Sports";
    models[1] = "Saloon";
    models[2] = "SUV";
    models[3] = "Hybrid";
    models[4] = "Estate";
  }  

  public TableBeanLazy()
  {
    System.out.println("-->  In constructor for TableBeanLazy");
  }

  @PostConstruct
  public void initialise()
  {
    System.out.println("-->  In initialise for TableBeanLazy");

    cars = new ArrayList<Car>();

    populateRandomCars(cars, 5);

    lazyModel = new LazyCarDataModel(cars);  
  }

  public Car getSelectedCar()
  {
    return selectedCar;
  }  

  public void setSelectedCar(Car selectedCar)
  {
    this.selectedCar = selectedCar;
  }

  public LazyDataModel<Car> getLazyModel()
  {
    return lazyModel;
  }

  private void populateRandomCars(List<Car> list, int size)
  {
    System.out.println("-->  In populateRandomCars for TableBeanLazy");

    for(int i = 0 ; i < size ; i++)
    {
      list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));  
    }  
  }  

  private String getRandomColor()
  {
    return colors[(int)(Math.random() * 10)];
  }

  private String getRandomManufacturer()
  {
    return manufacturers[(int) (Math.random() * 10)];
  }

  private String getRandomModel()
  {
    return models[(int)(Math.random() * 5)];
  }

  private int getRandomYear()
  {
    return (int)(Math.random() * 50 + 1960);
  }
}

表格初始显示时的输出

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy

点击搜索图标时输出

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
  at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62)
  at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
  at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
  at org.primefaces.component.api.UIData.processChildren(UIData.java:289)
  at org.primefaces.component.api.UIData.processPhase(UIData.java:261)
  at org.primefaces.component.api.UIData.processDecodes(UIData.java:227)
  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:506)
  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
  at org.primefaces.component.api.UIData.visitTree(UIData.java:639)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIForm.visitTree(UIForm.java:344)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
  at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
  at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
  at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
  at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62)
  at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
  at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
  at org.primefaces.component.api.UIData.processChildren(UIData.java:289)
  at org.primefaces.component.api.UIData.processPhase(UIData.java:261)
  at org.primefaces.component.api.UIData.processValidators(UIData.java:241)
  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:508)
  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
  at org.primefaces.component.api.UIData.visitTree(UIData.java:639)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIForm.visitTree(UIForm.java:344)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
  at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
  at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
  at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
  at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62)
  at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
  at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
  at org.primefaces.component.api.UIData.processChildren(UIData.java:289)
  at org.primefaces.component.api.UIData.processPhase(UIData.java:261)
  at org.primefaces.component.api.UIData.processUpdates(UIData.java:253)
  at org.primefaces.component.datatable.DataTable.processUpdates(DataTable.java:550)
  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:510)
  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
  at org.primefaces.component.api.UIData.visitTree(UIData.java:639)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIForm.visitTree(UIForm.java:344)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
  at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
  at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1229)
  at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy

这是有关CDI和JSF的第一条建议-切勿将CDI与JSF批注混合使用.

Here's your first piece of advice with CDI and JSF - Never mix CDI with JSF annotations.

您的问题是您将@javax.inject.Named CDI注释与javax.faces.bean.SessionScoped-JSF注释一起使用.

Your problem is that you use @javax.inject.Named a CDI annotation with javax.faces.bean.SessionScoped - a JSF annotation.

在CDI中,您可以这样做:

In CDI you would do:

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named(value = "tableBeanLazy")
@SessionScoped
public class TableBeanLazy {...}

请注意,以上内容将与JSF完美配合.

Note that the above will work perfectly fine with JSF.

您将以纯JSF方式进行操作:

In a pure JSF way you would do:

import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean(value = "tableBeanLazy")
@SessionScoped
public class TableBeanLazy {...}

我建议始终坚持使用CDI.但是,CDI当前不提供开箱即用的JSF @javax.faces.bean.ViewScoped的替代方法.要在CDI中获得它,请查看Apache Deltaspike.

I would recommend sticking with CDI all the way. However, CDI currently does not provide an alternative for the JSF @javax.faces.bean.ViewScoped out of the box. To get it in CDI look into Apache Deltaspike.