如何通过@ManagedProperty注释注入整个托管bean? [英] How to inject entire managed bean via @ManagedProperty annotation?

查看:341
本文介绍了如何通过@ManagedProperty注释注入整个托管bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 @ManagedProperty 注释将整个JSF托管bean注入另一个托管bean(非常类似于可以将@ManagedBean作为@ManagedProperty注入@WebServlet吗?,但我注入的是bean,而不是servlet)。这就是我正在做的事情:

I'm trying to inject entire JSF managed bean into another managed bean by means of @ManagedProperty annotation (very similar to Possible to inject @ManagedBean as a @ManagedProperty into @WebServlet?, but I'm injecting into a bean, not a servlet). This is what I'm doing:

@ManagedBean
public class Foo {
  @ManagedProperty(value = "#{bar}")
  private Bar bar;
}

@ManagedBean
public class Bar {
}

不起作用(JSF 2.0 / Mojarra 2.0.3):

Doesn't work (JSF 2.0/Mojarra 2.0.3):

SEVERE: JSF will be unable to create managed bean foo when it is 
requested.  The following problems where found:
- Property bar for managed bean foo does not exist. Check that 
  appropriate getter and/or setter methods exist.

是否有可能或我需要通过 FacesContext以编程方式进行此注入

Is it possible at all or I need to do this injection programmatically via FacesContext?

推荐答案

您需要添加setter和getter

You need to add setters and getters

@ManagedBean
public class Foo {
  @ManagedProperty(value = "#{bar}")
  private Bar bar;
  //add setters and getters for bar
  public Bar getBar(){
      return this.bar;
  }
  public void setBar(Bar bar){
      this.bar = bar;;
  }
}

FacesContext 将解析并注入它将使用setter注入的依赖项,因此适当的setter / getters应该在那里。否则它将找不到属性

When the FacesContext will resolve and inject dependencies it will use setters injection so appropriate setters/getters should be there.otherwise it won't find the property

这篇关于如何通过@ManagedProperty注释注入整个托管bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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