ViewScoped Bean cause NotSerializableException

imrabti picture imrabti · Oct 3, 2010 · Viewed 8.8k times · Source

Hello I'm using a ViewScoped Bean the Problem is that when call it I get the NotSerializableException.

This is the code of my Managed Bean :

@ManagedBean(name="demandesBean")
@ViewScoped
public class DemandesBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManagedProperty(value="#{demandeService}")
    private DemandeService demandeService; //A Spring Service

    @ManagedProperty(value="#{loginBean}")
    private LoginBean loginBean;

    private DemandeVO newDemande;

    @PostConstruct
    public void initData() {
        newDemande = new DemandeVO();
    }

    public void doAjouterDemande(ActionListener event) {
        demandeService.createDemande(newDemande, loginBean.getUsername());
        newDemande = new DemandeVO();
    }

    public List<DemandeVO> getListDemande() {
        return demandeService.getAllDemandesByUser(loginBean.getUsername());
    }

    public DemandeService getDemandeService() {
        return demandeService;
    }

    public void setDemandeService(DemandeService demandeService) {
        this.demandeService = demandeService;
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    public DemandeVO getNewDemande() {
        return newDemande;
    }

    public void setNewDemande(DemandeVO newDemande) {
        this.newDemande = newDemande;
    }
}

I Recieve The following Exception :

GRAVE: Exiting serializeView - Could not serialize state: com.bull.congesJBPM.serviceImpl.DemandeServiceImpl
java.io.NotSerializableException: com.bull.congesJBPM.serviceImpl.DemandeServiceImpl

Any fix for this problem ?? Please Help !

Answer

Arjan Tijms picture Arjan Tijms · Apr 28, 2012

Another problem is that MyFaces by default does serialization of state, even when state is being saved on the server (the default). This on its turn requires view scoped backing beans to be serializable.

The pros of this approach is that history is truly history. When you go back to a previous view version (using the back-button), you actually get the exact version of the backing bean at that time.

The con is that it seems to break injection of services (and unrelated to this problem, is a major performance hit). The exact same problems happens when injecting an EJB service.

There's a context parameter that you can put in web.xml to disable this behavior:

<context-param>
    <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
    <param-value>false</param-value>
</context-param>

See http://wiki.apache.org/myfaces/Performance

Incidentally, Mojarra has a similar setting but there the default is false.