JSF 1.X Backing Bean - EJB 3 Dependency Injection, (Glassfish) "Problem" and Solution 📎
In case the dependency between a JSR 1.2 backing bean and an EJB 3 doesn't work:
//a backing bean
public class OrderView{
@EJB private OrderGateway orderGateway;
public int getNrOfOrders(){
return orderGateway.get... //NullPointerException here
}
}
You should check the version of the web.xml. Dependency Injection was introduced with Java EE 5 and so the 2.5 Version of the Servlet spec. The header of the web.xml deployment descriptor should look like: <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
If you are using the 2.4 version: <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> Glassfish v2 will (correctly) ignore the @EJB annotation and you will get a NullPointerException at the first access.
See also this post for more details about the EJB 3.1 / JSF 2 synergy.