我试图通过@ManagedProperty
注释(非常类似于Possible to inject @ManagedBean as a @ManagedProperty into @WebServlet?将整个JSF托管bean注入另一个托管bean,但我注入了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
以编程方式进行此注入?
答案 0 :(得分:31)
您需要添加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 / getter应该在那里。否则它将找不到属性