users@jersey.java.net

[Jersey] Re: jersey-guice: Missing dependency for constructor

From: Gili <gili.tzabari_at_gmail.com>
Date: Thu, 13 Jan 2011 21:27:38 -0800 (PST)

Gili wrote:
>
> Hi,
>
> I created a test program according to
> http://jersey.java.net/nonav/apidocs/latest/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
> but for some reason whenever I try constructor injection I get:
>
> SEVERE: Missing dependency for constructor public
> foo.DoctorsResource(com.holdmyspot.search.server.Test) at parameter index
> 0
>
> Here is my config file (I verified this is being executed at load-time):
>
> public class GuiceConfig extends GuiceServletContextListener
> {
> @Override
> protected Injector getInjector()
> {
> return Guice.createInjector(new JerseyServletModule()
> {
> @Override
> protected void configureServlets()
> {
> bind(DoctorsResource.class);
> bind(Test.class).toInstance(new Test());
> serve("/*").with(GuiceContainer.class);
> }
> });
> }
> }
>
> And here is my resource:
>
> @Path("/doctors")
> public class DoctorsResource
> {
> private final Test test;
>
> @Inject
> public DoctorsResource(Test test)
> {
> this.test = test;
> }
> }
>
> I am fairly certain that Guice knows how to inject Test. Why then am I
> getting this error?
>
> Thanks,
> Gili
>

Adding @InjectParam to the constructor argument fixed the problem. That is:

        @Inject
        public DoctorsResource(@InjectParam Test test)
        {
                this.test = test;
        }

Why does this work? And why isn't this mentioned by the aforementioned
Javadoc? :)

Thanks,
Gili
-- 
View this message in context: http://jersey.576304.n2.nabble.com/jersey-guice-Missing-dependency-for-constructor-tp5920703p5920726.html
Sent from the Jersey mailing list archive at Nabble.com.