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
?
bean. ManagedBean) annotation in a class automatically registers that class as a resource with the JavaServer Faces implementation. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file.
You can use following scopes for a bean class: Application (@ApplicationScoped): Application scope persists across all users? interactions with a web application. Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.
1) BB: A backing bean is any bean that is referenced by a form. MB: A managed bean is a backing bean that has been registered with JSF (in faces-config. xml) and it automatically created (and optionally initialized) by JSF when it is needed.
JavaServer Faces (JSF) Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework. Managed bean contains the getter and setter methods, business logic, or even a backing bean (a bean contains all the HTML form value).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With