如何通过@ManagedProperty注释注入整个托管bean?

发布于 2024-10-20 07:14:24 字数 716 浏览 1 评论 0原文

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

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

@ManagedBean
public class Bar {
}

不起作用(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以编程方式进行此注入?

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 {
}

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2024-10-27 07:14:24

您需要添加setter和getter

@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。否则它将找不到该属性

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;;
  }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文