1
votes

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}" />

`

2
How do you know that the object is not changed?Salih Erikci
I debug the code. The object id is always the same. The values of the bean can be accessed from multiple pagesXunan Zhang
Can you please post the part where you debug the code.Salih Erikci
don't know how to post debug code. I just set a checkpoint at the my audit program, which captures some values of bean 'targetManager'. At the point, the bean's id is always the same number when threads from different pages and requests. The number id changes only after restart the app – Xunan Zhang 5 hours agoXunan Zhang

2 Answers

2
votes

CDI indeed creates a new object for each request but not for your servlet or what you are using.

Assuming you have a servlet, then the servlet instance is created only once and if you inject a request scoped bean into this servlet, then CDI is not able to exchange the bean instance within the servlet since this could result in unpredictable states. Thus CDI creates a proxy instance for the servlet which remains the same all the time the servlet lives. This is why you get only this single id for the bean instance, you are retrieving the id of the CDI proxy object, not of the underlying bean. When a new request comes in, CDI creates a new bean instance and backs the proxy with this bean for this specific request.

0
votes

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());
        }
}