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

查看:179
本文介绍了如何在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'.

使用SpringBeanFacesELResolver注入Spring服务.它在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服务可序列化不是一个好主意,也不是一个潜在的性能问题-Hibernate上下文,数据源(这些都注入到Spring服务中)应该怎么办?

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生命周期的情况下全面管理Backing 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天全站免登陆