users@jersey.java.net

Re: Inject Stateless Bean in jersey web resource

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Thu, 24 Apr 2008 16:47:42 +0200

On Thu, 2008-04-24 at 15:27 +0200, Paul Sandoz wrote:
> Hi Farjola,
>
> [just emailing the users list]
>
> Marc Hadley wrote:
> > Injection of EJBs isn't currently supported. We plan to add such support
> > in the future but for now you'll have to fall back to JNDI lookup instead.
> >
>
> I have not tried it but should possible if you don't mind depending on
> Spring, see Martin's blog [1] and my blog [2].
What I mentioned only with one sentence in my blog was the addition of
the AnnotationInjectable:

"jersey now provides an AnnotationInjectable, that can be provided for
some annotation, so that the injectable is asked for an instance of a
class if a field of some resource is annotated with the specified
annotation"

So you could override ServletContainer and especially

  initiate(ResourceConfig rc, WebApplication wa)

therein and add your EJBAnnotationInjectable for your @EJB annotation to
the WebApplication. EJBAnnotationInjectable can then override

  public Object getInjectableValue( Object o, Field f, EJB a )

and provide the desired EJB.

Right now you need jersey from the spring integration branch for this
[1], but Paul plans to merge this into the trunk these days.


Cheers,
Martin


[1] https://jersey.dev.java.net/svn/jersey/branches/spring-integration/jersey


>
> We plan to get Spring support into the 0.8 stable build. In the mean
> time you could create your own Servlet as shown in the blogs or take
> code from the branch.
>
> Paul.
>
> [1]
> http://www.javakaffee.de/blog/2008/04/21/jersey-spring-integration-mostly-complete/
> [2] http://blogs.sun.com/sandoz/entry/integrating_jersey_and_spring_take
>
> > Marc.
> >
> > On Apr 24, 2008, at 7:33 AM, Persa Zaloshnja wrote:
> >
> >> Hi all,
> >>
> >> I am creating a stateless bean using EJB3.0 and I'd like to inject
> >> that in jersey web resource. If I inject an ejb to an http/sip
> >> servlet, it works fine; but when I try the same in a jersey web res,
> >> it throws a nullpointer exception. A snippet of code for example:
> >>
> >>
> >> @Path("/helloworld")
> >> public class HelloWorldBean {
> >>
> >> //HERE INJECTING THE BEAN USING ANNOTATIONS.
> >> @EJB(name="ejb/mrbean")
> >> private IMrBean bean;
> >>
> >>
> >> @GET
> >> @ProduceMime("text/html")
> >> public String getClichedMessage() {
> >> //WHEN CALL FUNCTION getHello() - NULLPOINTER EXCEPTION THROWN!!!
> >> return bean.getHello();
> >> }
> >>
> >> (the packaging to deploy in glassfish is done in a .ear - jersey web
> >> resource in .war file and ejb in a .jar file)
> >>
> >> Is there anyone who has tried the same, is it possible to inject
> >> 'anything' to a jersey web resource ?
> >>
> >>
> >> Thank you in advance,
> >> Farjola
> >>
> >>
> >>
> >>
> >> Get news, entertainment and everything you care about at Live.com.
> >> Check it out!
> >
> > ---
> > Marc Hadley <marc.hadley at sun.com>
> > CTO Office, Sun Microsystems.
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: dev-help_at_jersey.dev.java.net
> >