问题描述
我试图通过 @ManagedProperty
注释(非常类似于 是否可以将@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/getter.否则它将找不到属性
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!