Skip to main content

@EJB injection works for Servlet, fails for WebServiceEndpoint

6 replies [Last post]
dlindsay_roamda...
Offline
Joined: 2008-03-19

Hi,

The following situation is a WAR existing inside an EAR, whereby the EAR contains a single @Stateless session bean deployed in a JAR. Access to the session bean using dependency injection already works within the EAR from a seperate JSF application. The issue is in getting dependency injection to work for the WebService WAR.

Here are snippets from my web.xml file:

RoamGatewaySessionServlet
com.roamdata.gateway.webservice.RoamGatewaySessionServlet
10

RoamGatewayService
RoamGatewayService
com.sun.xml.ws.transport.http.servlet.WSServlet

I have attached additional sun-*.xml files to show how the second servlet entry maps to a web service endpoint class. These entries were based on SUNs J2EE 5 sample code.

In the first entry, @EJB injection annotation for a Local Session Bean works inside the servlet-class.
The injection fails for the second entry.

Here is a snippet from my sun-jaxws.xml file:

Here you can see that the second servlet entry in my web.xml maps to the class com.roamdata.gateway.webservice.RoamGatewayImpl. Again, these were taken from SUN provided sample code.

However, @EJB injection does not work in this class. I get a NullPointerException. Here is how the dependency injection is defined:
@EJB
public GatewaySessionLocal gatewaySessionLocal;

Documentation clearly states that a web services end-point inside a web container should support injection for an EJB resource type (http://www.onjava.com/pub/a/onjava/2006/01/04/dependency-injection-java-...).

Why is @EJB dependency injection not working for this WebService class? It is in the same WAR as the servlet is.

See also:

http://forums.java.net/jive/thread.jspa?threadID=38298&tstart=30

David

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bhaktimehta
Offline
Joined: 2004-03-18

Hi do you have a testcase/NB project I can use?
Thanks,
Bhakti

dlindsay_roamda...
Offline
Joined: 2008-03-19

Hi,

Workaround/problem found. Please see:

http://forums.java.net/jive/thread.jspa?messageID=266239&#266239

David

Sahoo

Can you check the deployment descriptor version of web.xml? It should be
2.5 or above for annotations to work.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> Hi,
>
> The following situation is a WAR existing inside an EAR, whereby the EAR contains a single @Stateless session bean deployed in a JAR. Access to the session bean using dependency injection already works within the EAR from a seperate JSF application. The issue is in getting dependency injection to work for the WebService WAR.
>
> Here are snippets from my web.xml file:
>
>
> RoamGatewaySessionServlet
> com.roamdata.gateway.webservice.RoamGatewaySessionServlet
> 10
>

>
>
> RoamGatewayService
> RoamGatewayService
> com.sun.xml.ws.transport.http.servlet.WSServlet
>

>
> I have attached additional sun-*.xml files to show how the second servlet entry maps to a web service endpoint class. These entries were based on SUNs J2EE 5 sample code.
>
> In the first entry, @EJB injection annotation for a Local Session Bean works inside the servlet-class.
> The injection fails for the second entry.
>
> Here is a snippet from my sun-jaxws.xml file:
>
>
> > name='RoamGatewayService'
> implementation='com.roamdata.gateway.webservice.RoamGatewayImpl'
> url-pattern='/RoamGateway'/>
>

>
> Here you can see that the second servlet entry in my web.xml maps to the class com.roamdata.gateway.webservice.RoamGatewayImpl. Again, these were taken from SUN provided sample code.
>
> However, @EJB injection does not work in this class. I get a NullPointerException. Here is how the dependency injection is defined:
> @EJB
> public GatewaySessionLocal gatewaySessionLocal;
>
>
> Documentation clearly states that a web services end-point inside a web container should support injection for an EJB resource type (http://www.onjava.com/pub/a/onjava/2006/01/04/dependency-injection-java-...).
>
> Why is @EJB dependency injection not working for this WebService class? It is in the same WAR as the servlet is.
>
> See also:
>
> http://forums.java.net/jive/thread.jspa?threadID=38298&tstart=30
>
> David
> [Message sent by forum member 'dlindsay_roamdata_com' (dlindsay_roamdata_com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=265215
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

dlindsay_roamda...
Offline
Joined: 2008-03-19

Hi,

I have attached my web.xml. It is using version 2.5.

Remember, @EJB dependency injection works for the servlet in the same WAR. So the WAR/EJB/EAR relationships should be correct.

It believe its either a misconfiguration WRT the WebService endpoint or its a real bug. But I find it hard to believe that GF would have made it this long whereby an @WebService object cannot do @EJB injection.

Please see:

http://forums.java.net/jive/thread.jspa?messageID=265220

as well.

Thanks,
David

powerhouse_b
Offline
Joined: 2008-03-02

Hi.

I am having the same problems. @EJB throws an Exception, while @Resource does not inject the resource, it remains null (i wanted to inject WebServiceContex). Also, normal lookup doesn't seem to work. Did you find a solution or is this a bug?

dlindsay_roamda...
Offline
Joined: 2008-03-19

Hi,

I have two threads currently open on this issue, neither of which have been addressed properly. One individual suggested I list it as a bug.

What I now find funny is that this same code was working fine in JBoss. I had to resort to calling a Remote EJB interface when we migrated to Glassfish, and now that migration decision is biting me in the butt.

In other words, using a delegation pattern for Local Ejb Session bean calls from a Web Service was working code in JBoss. I am baffled that such a pattern is unsupported by the "daddy" of all application servers, SUN's AS 9.1, a.k.a. Glassfish. Can't quite understand it.

David