users@jersey.java.net

Get rid of requirement for no-arg constructor

From: Richard Wallace <rwallace_at_thewallacepack.net>
Date: Sat, 24 Nov 2007 23:00:55 -0800

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.

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?

Thanks,
Rich