users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 25 Mar 2009 10:13:16 +0100

On Mar 25, 2009, at 1:35 AM, Craig McClanahan wrote:

> 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.
>

Well said!

The plan of action for EE 6 integration is as follows:

   1) A session or singleton EJB can be a resource or provider class.
       In a previous email i showed a preview of this for a no-
interface view session bean deployed in the war.

   2) A JSR-299 artifact ("web bean") can be a resource or provider
class.

   3) Continue to support Spring/Guice integration.


Currently Jersey has to support some injection and life-cycle
management (since it does not depend on any specific IoC framework)
and it really only concentrates on the use cases specific to JAX-RS
and as Craig says it is not a general purpose IoC. The injection APIs/
SPIs i have referred to are used by Jersey to support injection of the
JAX-RS artifacts and i have exposed those for developers who can
utilize Jersey specific APIs to solve their problems.

In the long term i want to replace Jersey's specific injection and
life-cycle code with an IoC framework thus we can manage/maintain less
code. This code is possibly the most complex area of Jersey so i will
be glad to get rid of it!

Hope this clears things up a little,
Paul.