Adam Bien's Weblog

Monday Dec 07, 2009

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.


[my tweets]  Rss My book: Real World Java EE - Rethinking Best Practices

Kommentare:

Cheers for that, I can't believe I have spent about a week trying to work out why I kept getting a NullPointerException at the point of calling my sessionbean method. I have tried several examples but no one has entioned the web.xml version (2.5) before, a quick change and hey presto it works!

Gesendet von keith am February 14, 2010 at 11:31 AM CET #

Senden Sie einen Kommentar:
  • HTML Syntax: Ausgeschaltet
Interviews/About
My Recent Book
Java One 2009
CommunityOne East N.Y.C
JavaONE 2008 Interview
Search
...the last 150 posts
...the last 10 comments
greenfire.dev.java.net
Links
my.netbeans.org
Visitors
License