CDI bean 构造函数和@PostConstruct 多次调用 [英] CDI bean constructor and @PostConstruct called multiple times

查看:22
本文介绍了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 更改为托管 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?

我该如何解决这个问题?

How do I resolve this?

环境

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 带有 javax.faces.bean.SessionScoped 的 CDI 注释 - 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.

这篇关于CDI bean 构造函数和@PostConstruct 多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆