users@jersey.java.net

Re: [Jersey] Newbie question: How do I inject objects into resources

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Tue, 24 Mar 2009 17:35:00 -0700

David Fogel wrote:
> So, I start with the same basic question:
>
> Given that any non-trivial task involving writing a web app or service
> will almost certainly necessitate connecting JAX-RS-style resource
> classes with various data sources, back-end services,etc, what is the
> recommended _JAX-RS_ feature that enables this?
>
>
You don't see it in the JAX-RS spec because it's not there :-).

I'm not on the JSR-311 expert group, but have been talking with
Marc/Paul/others, and seeing what they've written on this topic, which I
would summarize as follows:

* JAX-RS defines annotations for it's own injection requirements
  (in the same way that JPA and EJB do so), but it is *not* itself
  intended to be, or provide, a general purpose injection framework
  for applications.

* JAX-RS is intended to be included in Java EE 6, and will therefore
  utilize whatever general purpose injection support is provided by
  the EE platform, rather than inventing its own.

* The direction that EE6 is going in this regard is JSR-299 (originally
  called "Web Beans", now called "Context and Dependency Injection
  for Java".

So, in an EE 6 environment, the answer to the "best practices for
general purpose dependency injection" will be "use JSR-299 APIs".
Presumably, in standalone servlet container cases, the various JAX-RS
implementations could also integrate with standalone JSR-299
implementations to provide API-compatible support there as well (and
this combination also seems likely IMHO to be included in the "Web
Profile" that the EE6 folks have been talking about).

It's just in the short term, then, that things are a little awkward.
That's one of the reasons that the Jersey folks went out of their way to
integrate with Spring and Guice now, and that's probably the direction I
would look for an app that needed general purpose IoC, and that needed
to be built and deployed sooner rather than later. The annotation-based
support for either of these will be at least be somewhat conceptually
similar to what JSR-299 ends up providing, enabling a conversion later
if you want to.

Craig