简体   繁体   English

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

[英]How to inject entire managed bean via @ManagedProperty annotation?

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). 我试图通过@ManagedProperty注释将整个JSF托管bean注入另一个托管bean(非常类似于将@ManagedBean作为@ManagedProperty注入@WebServlet?,但是我注入了一个bean,而不是一个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): 不起作用(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 ? 是否有可能或者我需要通过FacesContext以编程方式进行此注入?

You need to add setters and getters 您需要添加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;;
  }
}

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 FacesContext将解析并注入依赖关系时,它将使用setter注入,因此适当的setter / getter应该在那里。否则它将找不到属性

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

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