【问题标题】:Injecting JSF bean into Spring bean - impossible?将 JSF bean 注入 Spring bean - 不可能吗?
【发布时间】:2011-07-16 03:53:26
【问题描述】:

我有一个 JSF 2.0 bean:

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable
{
    protected String name;

    public String getName()
    {
        return name;
    }

    //....
}

我有一个 Spring 3.0 bean:

@Repository
public class Logins
{
    @ManagedProperty(value="#{loginBean}")
    protected LoginBean loginBean;

    public void recordLogin()
    {
         //... record in db that loginBean.getName() just logged in
    }
}

此代码不起作用,从未设置 Logins.loginBean。

或者(同样的问题,已简化)- 下面的代码会起作用吗?

@Repository
public class SpringBean
{
    @ManagedProperty(value="#{session.id}")
    protected String id;

    //....
}

ContextLoaderListener 和 RequestLoaderListener 在 web.xml 中声明。

是否可以将 JSF bean 注入到 Spring bean 中? (不使用另一个额外的框架)

或者我应该将我的 JSF bean 转换为 Spring bean 并在 faces-config.xml 中使用 DelegatingVariableResolver 技巧?我已经使用测试 Spring bean 对此进行了测试,并且可以正常工作。

【问题讨论】:

    标签: spring jsf dependency-injection


    【解决方案1】:

    在 spring 管理的 bean 中使用 JSF 注释不起作用。它不应该——你不应该从其他层的 web 层注入东西。它应该是相反的方式 - 将 spring 服务(或存储库)注入 Web 组件(jsf 托管 bean),并在它们上调用方法,将托管 bean 属性作为参数传递

    【讨论】:

    • 谢谢,这证实了我在谷歌搜索一个小时但没有成功后已经怀疑的事情:) 我最初的想法是将 LoginBean 转换为 Spring bean,但你没有从 web 层注入是对的.所以我会将 Spring“Logins”注入 JSF“LoginBean”。
    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 2014-07-01
    • 2011-08-16
    • 2015-04-15
    • 2012-05-21
    • 1970-01-01
    • 2011-12-10
    • 2011-09-05
    相关资源
    最近更新 更多