Injecting Managed beans in JSF 2.0
In JSF 2.0, a new @ManagedProperty annotation is used to dependency injection (DI) a managed bean into the property of another managed bean.
Let see a @ManagedProperty example :
MessageBean.java – A managed bean named “message“.
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="message")
@SessionScoped
public class MessageBean implements Serializable {
//business logic and whatever methods...
}
HelloBean.java – Inject the “message” bean into the “messageBean” property.
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
@ManagedProperty(value="#{message}")
private MessageBean messageBean;
//must povide the setter method
public void setMessageBean(MessageBean messageBean) {
this.messageBean = messageBean;
}
//...
}
In this example, it uses the @ManagedProperty annotation to DI the “message” bean (MessageBean.java) into the property (messageBean) of the “hello” bean (HelloBean.java) via setter method, setMessageBean().
Note
To make this injection successful, the inject property (messageBean) must provide the setter method.
To make this injection successful, the inject property (messageBean) must provide the setter method.
Download Source Code
Download it – JSF-2-Inject-Managed-Beans-Example.zip (10KB)
Sabeis tu verga
I agree with mkyong,
there is no problem of using sessionScoped, especially in a simple example like this.
If you have a large application and you keep using sessionScoped for all your beans than you should rethink your design.
Muito obrigado, sempre bem detalhista nas instruções.
please i can access to the value of my sessionbean(which is a sessionScoped)
please i need your help
thank you… very good.
Regards. When is a ManagedBeans and a ManagedProperty initialized?
Can somebody show an example for injecting a Map or HashMap with @ManagedProperty.
I am getting an error like ”
javax.el.ELException: Cannot convert #{userBean.refs of type class java.lang.String to interface java.util.Map
Here I am trying to inject refs as a Map. I am unable to get the reason. Please Help.
Hello Mkyong, congratulations for the excellent introductory tutorial for JSF 2. I have a question though, what is the difference of using the dependency injection or directly access another ManagedBean directly in xhtml? In this case we will be using two diferents managedBeans on the same xhtml. Thanks.
maybe my theory about it is still quite poor but I have noted that when i want to use data or method of the injected bean is called its constructor creating …it’s the right behaviour? what about if i want to call the same instance and not create a new one?
hi,
It’s posible injection in javax.faces.validator.Validator?
I need a DAO inside my validator for check that exist an element in the table with the same name.
Check constraint unique violation, whith hibernate an jsf.
Thanks for nothing, lol
This is the solution:
Hi
My question is what if the parameter sayWelcome in class MessageBean would be set through xhtml instead of setting in the class.
How would class HelloBean could access that parameter that is set and is belonged to the instantiated of class MessageBean?
This is normal problem when choosing from one dropdown menu one should result in list of certain items in 2:nd drop down menu.
Nice Tutorial Mkyong.
Thanks 🙂
Very simple and clear. Thanks in advance.
Thank you very much.
I tried to use it with @Named instead of @ManagedBean but it returns null, is there another annotation to use with @Named Beans or am I doing something wrong?
Thanks in advance.
IF there is no message bean instance does jsf create it not any annotation force to create?
THis was very useful to me. Thank you very much.
instead of @ManagedBean(name=”message”), can I use @Named(“message”)?
Note: JSF2 introduced the @ManagedBean annotation which was intended to minimise Faces configuration in XML files. Whilst this alleviates the need to declare JSF managed beans in XML it does not promote annotation consistency across all layers in the application.
it i good job.Thanks mkyong.
Whats wrong with sessionscoped?
ohh man! do you use sessionScoped? shame on you!
This is just an example. I used that for View Scoped and it worked fine. Please don’t condemn others work like this.
hi bro, i have ntg to shame for, it’s just one of the way to do DI in JSF. Please share your code and experience 🙂