如何在 JSF 管理的 bean 中使用 Spring 服务? [英] How to use Spring services in JSF-managed beans?

查看:21
本文介绍了如何在 JSF 管理的 bean 中使用 Spring 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSF 是 Java 世界中非常流行的技术,然而,与 Spring 的合作仍然是痛苦的,需要讨厌的"黑客.我目前遇到了这种黑客"之一的问题.

JSF is very popular technology in Java world, however, cooperation with Spring is still painfull and requires 'nasty' hacks. I have currently the problem with one of this 'hacks'.

Spring 服务是使用 SpringBeanFacesELResolver 注入的.在faces-config.xml中配置:

Spring services are injected using the SpringBeanFacesELResolver. It is configured in faces-config.xml:

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>

Spring 服务的注入很丑,但它是有效的:

The injection of Spring services is very ugly, but it is working:

@ManagedProperty(value="#{customerService}")
CustomerService customerService;

但是有问题.JSF 要求我管理的 bean 应该是可序列化的.这意味着,Spring 服务也必须是可序列化的,或者该字段应该是瞬态的.当该字段是瞬态时,注入不起作用(我在该字段中为空).在我看来,使 Spring 服务可序列化并不是一个好主意,而且存在潜在的性能问题 - 注入 Spring 服务的 Hibernate 上下文、数据源会发生什么?

But there are issues. JSF requires from me that the managed bean should be serializable. That means, that the Spring service must also be serializable, or the field should be transient. When the field is transient, the injection is not working (I have null in that field). And making Spring services serializable is in my opinion not a good idea and a potential performance issues - what should happen with Hibernate context, data sources, which all are injected into Spring service?

那么,将 Spring 服务与 JSF 托管 bean 一起使用的正确且不那么痛苦的方法是什么?

So, what is the correct and less painfull way of using Spring services withing JSF managed beans?

推荐答案

我在使用 org.springframework.web.jsf.el.SpringBeanFacesELResolver 时也遇到了很多问题.主要与不匹配的对象范围有关(Spring 没有等效于 JSF 的视图范围和对话范围).有些人还抱怨序列化问题.

I experienced a lot of issues with org.springframework.web.jsf.el.SpringBeanFacesELResolver, too. Mostly related to unmatching object scopes (Spring has no equivalent to JSF's view scope and conversation scope). Some people also complain about serialization problems.

我成功应用了本文提出的解决方案:http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/.

I successfully applied the solution proposed in this article: http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/.

就我而言,序列化不是问题,我只关心 bean 范围.我希望 JSF 能够在不干扰 Spring bean 生命周期的情况下全面管理支持 bean 生命周期.

In my case, serialization, was not a problem and I was only concerned with bean scopes. I wished JSF to fully manage the backing beans lifecycle without interference with Spring beans lifecycle.

我让 JSF 管理的 bean 加载 Spring 上下文并自动装配自己以从 JSF 上下文访问 Spring 管理的 bean.

I made JSF managed beans to load the Spring context and autowire themselves to access Spring-managed beans from the JSF context.

我开发了以下 JSF bean 超类:

I developed the following JSF bean superclass:

public abstract class AutowireableManagedBean {

    protected AutowireCapableBeanFactory ctx;

    @PostConstruct
    protected void init() {
        logger.debug("init");
        ctx = WebApplicationContextUtils
                .getWebApplicationContext(
                        (ServletContext) FacesContext.getCurrentInstance()
                                .getExternalContext().getContext())
                .getAutowireCapableBeanFactory();
        // The following line does the magic
        ctx.autowireBean(this);
    }
   ...
}

然后,我的具体 JSF 支持 bean 看起来像这样(我可以毫无问题地使用视图范围):

Then, my concrete JSF backing beans looked like this (I was able to use view scope without problems):

@ManagedBean
@ViewScoped
public class MyBackingBean extends AutowireableManagedBean {

    @Autowired
    private MyDao myDao;

这篇关于如何在 JSF 管理的 bean 中使用 Spring 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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