【问题标题】:Clean way to inject a Spring 3 bean into a JSF 2 managed bean?将 Spring 3 bean 注入 JSF 2 托管 bean 的干净方法?
【发布时间】:2011-12-18 16:15:12
【问题描述】:

我正在将我们当前的解决方案从 JSF 1.2 迁移到 JSF 2。因为我需要使用新的 View 范围,所以我正在使用 JSF 2 注释。这迫使我使用 JSF @ManagedProperty 注释而不是 Spring 的 @Autowired 注入 Spring bean

之前是这样的:

@Autowired private OneService oneService

现在是这样的:

@ManagedProperty(value="#{oneServiceImpl}")
private OneService oneService

您知道是否有一种方法可以注释托管属性而无需说明它们的 bean 名称?

谢谢!

【问题讨论】:

    标签: spring jsf-2 annotations


    【解决方案1】:

    您仍然可以将 Spring 与 JSF 2 一起使用。只需创建一个自定义 Spring 范围,然后可以将其用作 bean 的视图范围。

    @Named @Scope("view")
    public class MyBean {
    
        @Inject
        private MyManagedProperty oneService;
    
        //...
    
    }
    

    在此处窃取 View 范围的实现:http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/

    【讨论】:

      【解决方案2】:

      不,没有。 JSF 使用表达式语言 (EL) 来确定您按名称引用的类。使用名为ELResolver 的类,他获取传递的字符串,解释并进行适当的引用。 SpringBeanFacesELResolver 类提供两个框架之间的集成,拦截请求并将其传递给 Spring 的上下文,后者处理提供 ManagedBeans 所需的依赖关系,然后将其传递给 JSF 自己的 ELResolver。所以 JSF 需要 bean 的名称来知道要注入什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-01
        • 2013-01-23
        • 1970-01-01
        • 2023-03-27
        • 2012-01-27
        • 2011-03-25
        • 2015-03-11
        • 2011-07-01
        相关资源
        最近更新 更多