我在使用 Spring 托管服务的 JSF bean 方面遇到问题。我收到一条错误消息,JSF bean 中使用的 spring bean 不可序列化。
@ManagedProperty("#{customerService}")
private CustomerService customerService;
我无法使服务可序列化,因为它使用的 JdbcTemplate
本身不可序列化。此外,序列化具有应用程序范围的 Spring bean 根本没有任何意义,所以我不明白为什么有人的代码试图序列化它们。
我曾经使用 Spring 服务参与过 JSF 项目,没有出现这样的问题,所以这样的合作一定是可能的。但这个项目是根据示例项目从零开始做的,所以spring-JSF配合的配置肯定有问题,但不知道去哪里查找。
Spring for JSF 的配置是:
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
如何解决这个问题?
最佳答案
没有办法避免 JSF 序列化迷雾。甚至 ApplicationScoped bean 也会被序列化(当它们被注入(inject)到其他 bean 中时)。
但是解决方案是在 Spring 方面做出的。您必须使用范围代理。
要将 bean 包装到可序列化代理中,您必须添加到 bean 主体:
<aop:scoped-proxy proxy-target-class="true"/>
必须添加 spring aop 命名空间和 spring-aop
依赖项。
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
就是这样! bean 中将包含可序列化元素,即反序列化时从 Spring 上下文重新加载 bean 的代理。
这里唯一的迷雾是我必须创建cglib类级别代理。 JRE 代理无法工作,因为接口(interface)在反序列化期间不可用...我不完全理解为什么,但至少我有工作解决方案。
关于spring - JSF bean 和可序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497086/