users@jersey.java.net

Re: Get rid of requirement for no-arg constructor

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 26 Nov 2007 09:57:59 +0100

Richard Wallace wrote:
> Hello,
>
> I'm trying to get Guice working with Jersey and am running into one
> roadblock. The thing is that I really like to use constructor injection
> whenever possible. I want to do this with my resources. To that effect
> I've changed the GuiceProvider.getInstance() method from Christians blog
> post to
>
> public Object getInstance(ResourceProviderContext context)
> throws ContainerException {
> Object resource = getInjector(context).getInstance(resourceClass);
> context.injectDependencies(resource);
> return resource;
> }
>
> That way Guice is completely responsible for creating and injecting
> dependencies into the resource object without the need for a no-arg
> constructor.
>

It would be nice to 'mix-in' Jersey specific constructor parameter
injection with Guice constructor parameter injection. You can get the
Jersey specific per-request parameters using:

   ResourceProviderContext.getParameterValues

But Marc could not find a way in Spring to do the merging, perhaps in
Guice it is possible?


> This should work. But I'm running into a problem because Jersey is
> building an AbstractResource internally to track information about the
> resource class. This wouldn't be a problem except that one of the
> things that it is trying to find is a no-arg constructor. Why does it
> do that? I've search for references to the getConstructor() method and
> usages of the AbstractResourceConstructor class, but they are only used
> within AbstractResource and it doesn't actually do anything with the
> information, it seems to get it just to have it.
>
> Would it be possible to eliminate this completely and get rid of the
> no-arg constructor requirement?
>

Great to see you getting deep into the code :-)

As i understand this is a temporary issue. Jakub is working on an
abstract resource model, so that we can use meta-data other than
annotations and hide the validation of a resource from things that the
model (e.g. the runtime or for WADL generation), so he can provide more
accurate information on this aspect.

My understanding is that although some exceptions are printed out it
should not affect the working of resource providers. Is that the case
for you?

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109