4

CDI @RequestScoped bean life cycle

 2 years ago
source link: https://www.codesd.com/item/cdi-requestscoped-bean-life-cycle.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CDI @RequestScoped bean life cycle

advertisements

I defined a RequestScoped bean as below with CDI annotation. I have several pages which use the same bean. When I navigate from page to page. The bean obj keeps the same. Even when I change the session (login wth different user). The bean object is not changed.

According to RequestScoped definition, the bean instance should re-create for each new request. Anything I missed?

I am using JSF/Primefaces. The value input from create.xhtml page can be seen after navigating to detail.xhtml page. In backing bean, the value is not re-assigned.

Thanks,

Zhang

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

import javax.enterprise.context.RequestScoped;

import javax.inject.Named;

@Named("targetManager")

@RequestScoped

public class TargetManager implements Serializable { }

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

create.xhtml:

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

<ui:define name="contents">

<h:form id="createform">
  <h:outputLabel id="englishNameLabel">
     <h:outputText id="englishName" value="#{msg['view.label.englishname']}:" />
     <h:outputText id="englishNameStar" value="* " styleClass="mandatory" />
  </h:outputLabel>
  <p:inputText id="englishTitle" value="#{targetManager.selectedTarget.englishName}" >
    <f:validator id="englishNameValidate1" validatorId="duplicateValidator" />
  </p:inputText>`

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

detail.xhtml:

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

  <ui:define name="contents">

  <p:scrollPanel id="scrollPanel" styleClass="contentPanel ntb" mode="native">

     <h:outputText id="englishName" value="#{msg['.view.label.englishname']}:"/>
       <h:outputText id="englishNameValue" value="#{targetManager.selectedTarget.englishName}" />


You can check if it is recreated by outputting the time it is created. You will see that it outputs different time everytime which means it is recreated on every request.

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named("targetInstructionManager")
@RequestScoped
public class TargetInstructionManager implements Serializable {
    public TargetInstructionManager (){
            System.out.println(System.currentTimeMillis());
        }
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK