微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JSF-会话范围的托管bean没有在会话反序列化上重新注入依赖项

如何解决JSF-会话范围的托管bean没有在会话反序列化上重新注入依赖项

尽管Bozho提供的解决方案可行,但我不想将代理对象引入当前未使用它们的应用程序中。我的解决方案不理想,但是可以完成工作。

我将瞬变场留在原处:

transient private ApplicationData _applicationData;

我还保留了setter,以便JSF可以SessionData在首次创建对象时初始设置引用:

public void setApplicationData(ApplicationData applicationData) {
    _applicationData = applicationData;
}

我所做的更改是使用getter方法进行的。SessionData现在,该对象中的方法需要停止直接访问该_applicationData字段,而是通过getter获取引用。吸气剂将首先检查空引用。如果为null,则通过来获取托管beanFacesContext。这里的约束是,FacesContext仅在请求的生命周期内可用。

/**
 * Get a reference to the ApplicationData object
 * @return ApplicationData
 * @throws IllegalStateException May be thrown if this method is called
 *  outside of a request and the ApplicationData object needs to be
 *  obtained via the FacesContext
 */
private ApplicationData getApplicationData() {
    if (_applicationData == null) {
        _applicationData = JSFUtilities.getManagedBean(
            "applicationData",  // name of managed bean
            ApplicationData.class);
        if (_applicationData == null) {
            throw new IllegalStateException(
                "Cannot get reference to ApplicationData object");
        }
    }
    return _applicationData;
}

如果有人在意,这是我的getManagedBean()方法代码

/**
 * <p>Retrieve a JSF managed bean instance by name.  If the bean has
 * never been accessed before then it will likely be instantiated by
 * the JSF framework during the execution of this method.</p>
 * 
 * @param managedBeanKey String containing the name of the managed bean
 * @param clazz Class object that corresponds to the managed bean type
 * @return T
 * @throws IllegalArgumentException Thrown when the supplied key does
 *  not resolve to any managed bean or when a managed bean is found but
 *  the object is not of type T
 */
public static <T> T getManagedBean(String managedBeanKey, Class<T> clazz)
        throws IllegalArgumentException {
    Validate.notNull(managedBeanKey);
    Validate.isTrue(!managedBeanKey.isEmpty());
    Validate.notNull(clazz);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (facesContext == null) {
        return null;
    }
    Validate.notNull(facesContext.getApplication());
    ELResolver resolver = facesContext.getApplication().getELResolver();
    Validate.notNull(resolver);
    ELContext elContext = facesContext.getELContext();
    Validate.notNull(elContext);
    Object managedBean = resolver.getValue(
        elContext, null, managedBeanKey);
    if (!elContext.isPropertyResolved()) {
        throw new IllegalArgumentException(
            "No managed bean found for key: " + managedBeanKey);
    }
    if (managedBean == null) {
        return null;
    } else {
        if (clazz.isinstance(managedBean)) {
            return clazz.cast(managedBean);
        } else {
            throw new IllegalArgumentException(
                "Managed bean is not of type [" + clazz.getName() +
                "] | Actual type is: [" + managedBean.getClass().getName()+
                "]");
        }
    }
}

而且不要接听我的验证电话。开发完成后,我将其淘汰!:)

解决方法

我不确定我在做什么是错误的,还是只是错过了某个地方的注释或配置项。情况如下:

我有一个JSF应用程序,其会话范围的bean名为SessionDataApplicationData在创建时,此Bean
注入了一个应用程序范围的Bean引用(类型)。首次创建会话时,此方法正常。依赖项注入是通过文件中的<managed-bean>元素完成的,faces- config.xml如下所示:

<managed-bean>
    <managed-bean-name>sessionData</managed-bean-name>
    <managed-bean-class>my.package.SessionData</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>applicationData</property-name>
        <property-class>my.package.ApplicationData</property-class>
        <value>#{applicationData}</value>
    </managed-property>
</managed-bean>
<managed-bean>
    <managed-bean-name>applicationData</managed-bean-name>
    <managed-bean-class>my.package.ApplicationData</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

因为序列化时让我的SessionData对象包含ApplicationData对象没有意义,所以我在对象中将ApplicationData引用标记为瞬态SessionData

transient private ApplicationData applicationData;

直到停止Web应用程序(在我的Tomcat
6.x容器中)并序列化会话之前,一切都很好。当我重新启动应用程序并且反序列化会话时ApplicationData,JSF不会重新插入对我的引用。我知道反序列化应该使瞬态字段没有值。
是否有一种方法可以向JSF发出信号,告知该会话范围的对象要求在反序列化之后再次设置其依赖项?

我将MyFaces JSF 1.2和Tomcat 6.0.26用作Web应用程序容器。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。