@EJB injection within JSF managed-bean returns null object

Hi there,

there are some posts out there about @EJB injection but after many tries, I still can't get it to work.

The injected object is always null.

I got the lookup method working yesterday but amazingly it's not working anymore today after re-deploying.

Env:

- sun-jdk-1.5.0.10

- jboss AS 4.0.4

Packaging:

--EAR

-JAR (EJB app)

META-INF/

--ejb-jar.xml

--persistence.xml

src/

--session/

-IBookingManager.java

-bean/

BookingManagerBean.java

-WAR (web tier)

src/

--business/

-Disp.java

--session/

WebContent/

--META-INF/

--WEB-INF/

-faces-config.xml

-web.xml

--index.jsp

--IBookingManager.java

EJB app. files:

persistence.xml:

<?xml version="1.0"?>

<persistence version="1.0">

<persistence-unit name="EJB-EX">

<jta-data-source>java:EJB-EX-DS</jta-data-source>

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />

<property name="hibernate.show_sql" value="true"></property>

</properties>

</persistence-unit>

</persistence>

ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>

<ejb-jar></ejb-jar>

session/IBookingManager.java:

package session;

publicinterface IBookingManager{

public String getHW();

//some other methods

}

session/bean/BookingManagerBean.java

package session.bean;

import javax.ejb.Local;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

import session.IBookingManager;

@Stateless

@Local

publicclass BookingManagerBeanimplements IBookingManager{

@PersistenceContext(name="EJB-EX")

private EntityManager em;

public String getHW(){

return"kind of hello world ...";

}

//other methods definition using 'em' object

}

web tier files:

WebContent/WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.5" 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_5.xsd">

<display-name>

bibgoeson</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.jsf</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<listener>

<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>

</listener>

<servlet>

<servlet-name>abcDEF</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>abcDEF</servlet-name>

<url-pattern>*.jsf</url-pattern>

</servlet-mapping>

</web-app>

WebContent/WEB-INF/faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>

<faces-config 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-facesconfig_1_2.xsd"

version="1.2">

<application>

<locale-config>

<default-locale>en</default-locale>

</locale-config>

</application>

<!-- BEANS -->

<managed-bean>

<managed-bean-name>dis</managed-bean-name>

<managed-bean-class>business.Disp</managed-bean-class>

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

<!-- /BEANS -->

</faces-config>

WebContent/index.jsp:

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<html>

<head>

<title>title</title>

</head>

<body>

<f:view>

<h:form>

<h1>return</h1>

<h:outputText value="#{dis.first}" />

</h:form>

</f:view>

</body>

</html>

src/session/IBookingManager.java is the same as in EJB app

src/business/Disp.java:

package business;

import javax.ejb.EJB;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import session.IBookingManager;

publicclass Disp{

// <A>

//@EJB public IBookingManager bm;

// <A/>

private String first;

public String getFirst(){

// <B>

IBookingManager bm =null;

try{

Context c =new InitialContext();

c =new InitialContext();

bm = (IBookingManager) c.lookup("bibgoesonEAR/BookingManagerBean/local");

}catch (NamingException e){

// TODO Auto-generated catch block

e.printStackTrace();

}

// </B>

if ( bm ==null )

first ="bm is null.";

else

first ="go on";

return first;

}

publicvoid setFirst(String first){

this.first = first;

}

}

So from above, I'm using either part <A> or <B>.

When using <A>, the web page prints "bm is null."

When using <B>, I get a java.lang.ClassCastException on the lookup line. I can't explain because it was working yesterday..

The question is: why this weird behavior with the injection and how to make it work?

I think I'm using regular design with some sort of Facade design pattern where the managed-bean injects

the interface of the session-bean, in turn using a persistence context, nothing special huh?

Why the lookup method doesn't work anymore too?

Thanks

[9576 byte] By [andromedea] at [2007-11-26 18:49:05]
# 1
Does c.lookup("bibgoesonEAR/BookingManagerBean/local"); method return something ?
Alexandre_Jaqueta at 2007-7-9 6:23:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
No it doesn't. The object is null.It was working a while ago with the lookup method but not anymore.I'm now stuck with both methods.
andromedea at 2007-7-9 6:23:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
lookup() method is back. It seems like we _have to_ place a jndi.properties file at src/ folder of the wep app otherwise this just can't work.I thought it wasn't necessary as both app are in the same EAR. Could anyone confirm?Still can't make @EJB injection to work though :
andromedea at 2007-7-9 6:23:07 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...