Injecting non-serializable application scoped bean as managed property of serializable session scoped bean in a cluster

有些话、适合烂在心里 提交于 2019-11-29 07:10:07

问题


I have the following managed beans :

@ApplicationScoped
public class ApplicationBean {
    // ...
}
@SessionScoped
public class SessionBean implements Serializable {

    @ManagedProperty("#{applicationBean}")
    private ApplicationBean applicationBean;

    // ...
}

This is deployed to a server cluster with several nodes. What will happen when the HTTP session will be serialized on another node?

ApplicationBean is not serialized because it doesn't implement Serializable. Will it be re-injected by @ManagedProperty? Or will it actually be serialized somehow?


回答1:


What will happen when the HTTP session will be serialized on another node?

All HTTP session attributes will be serialized as well, including session scoped JSF managed beans. Any bean properties which are not serializable will be skipped. During deserialization on another node, you will face a NotSerializableException on all bean properties which are not serializable. Marking the property transient will fix that exception, but the property will still remain null after deserialization.


Will it be re-injected by @ManagedProperty? Or will it actually be serialized somehow?

Nope. It won't be re-injected. You have to take care of this manually in case of @ManagedProperty.

One somewhat naive and error-prone way is getting rid of @ManagedProperty and performing lazy loading in the getter (thus, act like a proxy yourself):

private transient ApplicationBean applicationBean;

public ApplicationBean getApplicationBean() {
    if (applicationBean == null) { 
        FacesContext context = FacesContext.getCurrentInstance();
        applicationBean = context.getApplication().evaluateExpressionGet(context, "#{applicationBean}", ApplicationBean.class);
    }

    return applicationBean;
}

and use the getter throughout the code instead referencing the property directly.

The better way is to make it an EJB or a CDI managed bean. They're fully transparently created and injected as serializable proxies and you never need to worry about serialization on them.

Thus, either make it an EJB:

import javax.ejb.Singleton;

@Singleton
public class ApplicationBean {
    // ...
}
import javax.ejb.EJB;
import.javax.faces.bean.ManagedBean;
import.javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {

    @EJB
    private ApplicationBean applicationBean;

    // ... (no setter/getter necessary!)
}

Or, make them both CDI managed beans:

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

@Named
@ApplicationScoped
public class ApplicationBean {
    // ...
}
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class SessionBean implements Serializable {

    @Inject
    private ApplicationBean applicationBean;

    // ... (also here, no setter/getter necessary!)
}


来源:https://stackoverflow.com/questions/20067698/injecting-non-serializable-application-scoped-bean-as-managed-property-of-serial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!