将JSF托管的bean批注与Spring Boot集成 [英] Integrating JSF managed bean annotations with Spring Boot

查看:214
本文介绍了将JSF托管的bean批注与Spring Boot集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSF 2.2中使用Spring Boot.我的问题是我可以从javax.annotation.ManagedBean创建@ManagedBean,并且在运行应用程序时它可以在index.xhtml中运行,但是当我要使用javax.faces.bean.ManagedBean时却不显示该值.两者之间有什么区别?为什么我不能使用javax.faces.bean.ManagedBean? (我没有web.xml文件,所有文件都在类中配置)

I use Spring boot with JSF 2.2. My problem is that I can create @ManagedBean from javax.annotation.ManagedBean and it is working in my index.xhtml when I run the app, but when I want to use javax.faces.bean.ManagedBean is not displaying the value. What's the difference between those two? Why I can't use the javax.faces.bean.ManagedBean? ( I don't have web.xml file, all is configured in classes)

推荐答案

javax.annotation.*注释旨在从经典的JSF注释过渡到CDI方法. Spring框架具有读取某些CDI注释的能力,因此这可能是此注释起作用"的原因.但是,总体而言,CDI的趋势是使用@Named.

The javax.annotation.* annotations are meant to be a move from the classic JSF annotations to a CDI approach. The Spring Framework has the ability to read some CDI annotations, so that could be the reason why this annotation "works". However, the trend in CDI is to use @Named, overall.

在Spring Boot应用程序中,它是Spring扫描注释的文件,而不是JSF.因此,即使您可能认为该应用程序可与@ManagedBean一起使用,您也会看到@*Scoped注释是无用的,因为所有创建的bean恰好都是单例,这是Spring的默认范围.

In a Spring Boot application, it's Spring the one scanning your annotations, not JSF. So, even you could think that the application works with @ManagedBean, you'll see that the @*Scoped annotations are useless, because all the created beans happen to be singletons, which is Spring's default scope.

最后,我做出的选择是使用普通Spring注释和作用域.由于Spring缺少JSF视图范围,因此也可以使用自定义范围进行仿真.

In the end the choice I made was to use vanilla Spring annotations and scopes. As Spring lacks the JSF view scope, also a custom scope to emulate it.

MyBean.java:

@Component
@Scope("view")
public class MyBean {
  //Here it goes your logic
}

ViewScope.java:

public class ViewScope implements Scope {

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
        if (viewMap.containsKey(name)) {
            return viewMap.get(name);
        } else {
            Object object = objectFactory.getObject();
            viewMap.put(name, object);

            return object;
        }
    }

    @Override
    public String getConversationId() {
        return null;
    }

    @Override
    public void registerDestructionCallback(String arg0, Runnable arg1) {

    }

    @Override
    public Object remove(String name) {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }

    @Override
    public Object resolveContextualObject(String arg0) {
        return null;
    }

}

使用 CustomScopeConfigurer :

@Bean
public static CustomScopeConfigurer viewScope() {
    CustomScopeConfigurer configurer = new CustomScopeConfigurer();
    configurer.setScopes(
            new ImmutableMap.Builder<String, Object>().put("view", new ViewScope()).build());
    return configurer;
}

最后,不要忘了在您的faces-config.xml中添加Spring EL解析器,以通过EL表达式使Spring Bean可用:

Finally, do not forget to add the Spring EL resolver in your faces-config.xml to make the Spring beans available through EL expressions:

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

另请参见:

  • Why are there different bean management annotations
  • Backing beans (@ManagedBean) or CDI Beans (@Named)?
  • Configuring Spring Boot with JSF

这篇关于将JSF托管的bean批注与Spring Boot集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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