JSF bean和可序列化问题 [英] JSF beans and serializability issue

查看:211
本文介绍了JSF bean和可序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用Spring托管服务的JSF Bean有问题.我收到一个错误消息,说JSF bean中使用的spring bean无法序列化.

I have problem with JSF beans using Spring managed services. I got an error telling, that spring bean used in JSF bean is not serializable.

@ManagedProperty("#{customerService}")
private CustomerService customerService;

我无法使服务可序列化,因为它使用的是JdbcTemplate,该服务本身无法序列化.而且,对具有应用程序范围的Spring bean进行序列化完全没有意义,所以我不明白为什么有人的代码试图对其进行序列化.

I can't make the service serializable, because it is using JdbcTemplate which itself isn't serializable. Moreover, serializing Spring beans which have application scope makes no sense at all, so I don't understand, why someone's code is attempting to serialize them.

我已经使用Spring服务与JSF项目一起工作,并且没有此类问题,因此这种合作必不可少.但是该项目是基于示例项目从头开始构建的,因此spring-JSF合作的配置一定存在问题,但是我不知道在哪里搜索.

I have worked with JSF project using Spring services, and there were no such issues, so such cooperation must be possible. But this project is made from scratch based on example projects, so there must be something wrong with the configuration of spring-JSF cooperation, but I don't know where to search.

Spring for JSF的配置是:

The configuration of Spring for JSF is:

<!-- JSF and Spring are integrated -->
<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>

如何解决此问题?

推荐答案

无法避免JSF序列化错误.甚至ApplicationScoped Bean也被序列化(当它们注入到其他Bean中时).

There is no way to avoid JSF serialization mist. Even ApplicationScoped beans are serialized (when they are injected into other beans).

但是解决方案是在春季进行的.您必须使用作用域代理.

But the solution was made on the Spring side. You have to use scoped proxy.

要将bean包装到可序列化的代理中,您必须添加到bean主体中:

To wrap the bean into serializable proxy you have to add to bean body:

<aop:scoped-proxy proxy-target-class="true"/>

必须添加spring aop命名空间和spring-aop依赖项.

The spring aop namespace and spring-aop dependency must be added.

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

就这样! Bean中将包含序列化元素,该代理将在反序列化时从Spring上下文重新加载Bean.

And this is it! In the bean will be the serializable element, the proxy that will re-load bean from Spring context on deserialization.

这里唯一的迷惑是我必须创建 cglib 类级代理. JRE代理无法正常工作,因为在反序列化期间该接口不可用...我不完全理解为什么,但是至少我有可行的解决方案.

The only mist here is that I have to create cglib class-level-proxy. JRE proxy was not working because the interface was not available during deserialization... I don't understand fully why but I have working solution at least.

这篇关于JSF bean和可序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆