I have an EJB (PersonManager) in the Enterprise Application modul, which injects another EJB (Person):
@Stateful
public class PersonManager implements PersonManagerLocal {
@EJB
private PersonLocal person;
@Override
public void setPersonName(String name) {
person.setName(name);
}
@Override
public String getPersonName() {
return person.getName();
}
}
I want to use the PersonManager EJB in a JSF web app. I define it in the faces-config.xml:
<managed-bean>
<managed-bean-name>personManager</managed-bean-name>
<managed-bean-class>ejb.PersonManager</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
The problem is that, the injection of the PersonLocal EJB doesn’t happen. The person property is always NULL. What did I wrong?
But if I inject the PersonManager in a JSF managed bean like this:
@ManagedBean
@RequestScoped
public class Index {
@EJB
private PersonManagerLocal personManager;
....
IT WORKS. I need the first scenario, please help me 😀
You are mixing the responsibilities of EJBs and JSF managed beans. The
faces-config.xmlregisters only JSF artifacts, such as managed beans and not EJBs. Your registration infaces-config.xmldoes basically exactly the same as
In other words, you’re registering the class as a JSF managed bean which is available in views by
#{personManager}. This does not represent the same instance as is managed by the EJB container. You can and should not usefaces-config.xmlto register EJBs. There you use the annotations from thejavax.ejbpackage for, such as@Statelessand@Stateful. That’s all you need to register an EJB.Actually, registering JSF managed beans in
faces-config.xmlis an old JSF 1.x way which has in JSF 2.x been replaced by the new@ManagedBeanannotation.Update the proper approach would be:
View (the Facelets file):
Controller (the JSF managed bean):
Model (the JPA entity):
Service (the stateless EJB):