Serialization of ManagedProperty

2020-07-23 08:58发布

问题:

We have the following problem with JSF's @ViewScoped and @ManagedProperty: we have ManagedBeans that basically look as follows:

@ManagedBean
@SessionScope
public class SessionConfig implements Serializable
{
    // ...
}

and

@ManagedBean
@ViewScope
public class SomeController implements Serializable
{
    @ManagedProperty( value="#{sessionConfig}" )
    private SessionConfig sessionConfig;
    // public getter and setter

    // ...
}

The controller is serialized after the request has been processed, as expected. I expected that the @ManagedProperty sessionConfig would be handled specially in serialization, in particular, that it will be "relinked" after deserialization. However, it turns out that after deserialization sessionConfig is merely a stale clone of the actual SessionConfig-Bean.

Questions:

  1. Is this the expected behavior?
  2. What can we do to make JSF re-evaluate the @ManagedProperty after deserialization?

Currently, we "manually" re-evaluate all managed properties after deserialzation. It works but, clearly does not seem right.

Thank you!

回答1:

A solution is to avoid @ManagedProperty in @ViewScoped beans and evaluate the EL expression on demand. @ViewScoped beans will be stored in Session (and thus serialized) at the end of every request.

FacesContext ctx = FacesContext.getCurrentInstance();
ctx.getApplication().evaluateExpressionGet(ctx, "#{sessionConfig}", SessionConfig.class)

Note that this may seriously affect perfromance if the expression has to be evaluated frequently.

A better aproach might be to provide custom serialization methods as described here: http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html This way the expression could be resolved automatically every time the bean is deserialized.