Spring JSF 集成:如何在 JSF 托管 bean 中注入 Spring 组件/服务? [英] Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
问题描述
我知道托管 bean 的工作方式类似于控制器,因为您唯一的任务是将视图层与模型链接".
I understand that a managed bean works like a controller, because your only task is "link" the View Layer with Model.
要将 bean 用作托管 bean,我必须声明 @ManagedBean
注释,这样我就可以直接与 bean 通信 JSF.
To use a bean as a managed bean I must declare @ManagedBean
annotation, doing that I can communicate JSF with bean directly.
如果我想在这个 managedBean 中注入一些组件(来自 Spring),我有两种可能的方法:
If I want to inject some component (from Spring) in this managedBean I have two possibles ways:
在 ManagedBean 中选择属性(如BasicDAO dao")并在属性上方声明
@ManagedProperty(#{"basicDAO"})
.这样做,我正在从 ManagedBean 中的 Spring 注入 bean"basicDAO"
.
Choose the property in ManagedBean (like "BasicDAO dao") and declare
@ManagedProperty(#{"basicDAO"})
above the property. Doing it, i'm injecting the bean"basicDAO"
from Spring in ManagedBean.
在 ManagedBean 类中声明 @Controller,然后我将有 @ManagedBean
和 @Controller
注释,一起.在属性 "BasicDAO dao"
中,我必须使用 Spring 中的 @Autowired
.
Declared @Controller in ManagedBean Class, then i'll have @ManagedBean
and @Controller
annotations, all together. And in property "BasicDAO dao"
i must use @Autowired
from Spring.
我的理解正确吗?
推荐答案
通过简单地从 SpringBeanAutowiringSupport
扩展您的 JSF bean,还有另一种方法可以在 JSF 管理的 bean 中使用 Spring 管理的 bean,并且 Spring 将处理依赖注入.
There is another way to use Spring-managed beans in JSF-managed beans by simply extending your JSF bean from SpringBeanAutowiringSupport
and Spring will handle the dependency injection.
@ManagedBean // JSF-managed.
@ViewScoped // JSF-managed scope.
public class GoodBean extends SpringBeanAutowiringSupport {
@Autowired
private SpringBeanClass springBeanName; // No setter required.
// springBeanName is now available.
}
这篇关于Spring JSF 集成:如何在 JSF 托管 bean 中注入 Spring 组件/服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!