简体   繁体   English

将Spring 3 bean注入JSF 2托管bean的干净方法?

[英]Clean way to inject a Spring 3 bean into a JSF 2 managed bean?

I'm migrating our current solution from JSF 1.2 to JSF 2. As I need to use the new View scope I'm using JSF 2 annotations. 我正在将我们当前的解决方案从JSF 1.2迁移到JSF 2.因为我需要使用新的View范围,所以我正在使用JSF 2注释。 That forced me to inject the Spring beans using the JSF @ManagedProperty annotation instead of Spring's @Autowired 这迫使我使用JSF @ManagedProperty注释而不是Spring的@Autowired注入Spring bean

Before it was something like this: 之前它是这样的:

@Autowired private OneService oneService

And now it's like: 而现在它就像:

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

Do you know if is there a way to annotate the managed properties without needing to state their bean name? 您是否知道是否有办法在不需要声明其bean名称的情况下注释托管属性?

Thanks! 谢谢!

No, there isn't. 不,没有。 JSF makes use of Expression Language (EL) to determine which class you refer by name. JSF使用表达式语言(EL)来确定您按名称引用的类。 Using a class called ELResolver he takes the String passed, interprets and makes the appropriate reference. 使用一个名为ELResolver的类,他接受传递的String,解释并做出适当的引用。 The class SpringBeanFacesELResolver provides integration between the two frameworks intercepts the request and passing it to the context of Spring, which handles the dependencies required to provide the ManagedBeans, who then passes it to the JSF's own ELResolver. SpringBeanFacesELResolver类提供了两个框架之间的集成,拦截请求并将其传递给Spring的上下文,Spring处理提供ManagedBeans所需的依赖关系,然后ManagedBeans将其传递给JSF自己的ELResolver。 So JSF needs the name of the bean to know what to inject. 所以JSF需要bean的名称来知道要注入什么。

You can still use Spring with JSF 2. Just create a custom Spring scope which can then be used as the view scope for your beans. 你仍然可以使用Spring和JSF 2.只需创建一个自定义的Spring作用域,然后可以将其用作bean的视图作用域。

@Named @Scope("view")
public class MyBean {

    @Inject
    private MyManagedProperty oneService;

    //...

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM