Use in combination with private module... And expose what you want to use...
On Mar 13, 2012 7:06 PM, "Warren" <[email protected]> wrote:
> I reduced ExternalDataSourceModule down to this:
>
> protected void configure()
> {
> bind(FooBar.class).to(FooBarImpl.class);
> }
>
> and reduced PushServletModule down to this:
>
> protected void configureServlets()
> {
> /* bind the REST resources */
> bind(ItemResource.class);
> /* bind jackson converters for JAXB/JSON serialization */
>
> bind(MessageBodyReader.class).to(JacksonJsonProvider.class);
> bind(MessageBodyWriter.class).to(JacksonJsonProvider.class);
>
> Map<String, String> initParams = new HashMap<String, String>();
> initParams.put("com.sun.jersey.config.feature.Trace", "true");
> initParams.put("com.sun.jersey.config.property.packages",
> "com.warren.pushlocal.rest.resources");
> serve("*").with(GuiceContainer.class, initParams);
> }
>
> I am injecting FooBar into ItemResource. I get the same type of error
> message: "It was already configured on one or more child
> injectors or private modules".
>
> I reduce it down further by replacing bind(ItemResource.class) with
> bind(Foo.class) and I inject FooBar into Foo and I get no errors.
>
> I am using guice to configure/manage jersey, so I am guessing
> something is going on under the covers in jersey that does not like
> having a resource injected when it is configured in a child injector,
> or as you said:
>
> "you force bindings sooner than they would be in a normal binding
> context"
>
> But, you mentioned that if I use a single injector that I would "move
> the problem that would be exposed later in the single-injector case to
> injector-creation time". When I use a single injector, the injection I
> make into ItemResource works fine.
>
> I guess the bottom line is that Jersey REST resources should be
> configured in a single or parent injector and not a child injector ?
> If that is the case, then I will create a new post that addresses the
> reason why I was using a child injector in the first place.
>
> Any insight would be appreciated.
>
> Thanks,
>
> Warren
>
>
> On Mar 13, 5:11 am, Christian Edward Gruber <[email protected]>
> wrote:
> > Can you replicate this with extremely minimalist
> ExternalDataSourceModule() and PushServletModule()s? The devil's in the
> details here. What is being bound may have an impact.
> >
> > One thing worth noting is that, as I recall, when you
> createChildInjector() you force bindings sooner than they would be in a
> normal binding context - I believe this is to resolve things in the parent
> so they can be available to the chile. However, this can move the problem
> that would be exposed later in the single-injector case to
> injector-creation time, which may be why these look different. If you
> could empty out these modules until you have versions that both invoke this
> problem and you can post, that would help, since then we can see what's
> actually going wrong in the bindings.
> >
> > Christian.
> >
> > On Mar 13, 2012, at 1:04 AM, Warren wrote:
> >
> >
> >
> >
> >
> >
> >
> > > I have implemented GuiceServletContextListener like this:
> >
> > > @Override
> > > protected Injector getInjector()
> > > {
> > > Injector parentInjector = Guice.createInjector(new AbstractModule()
> > > {
> >
> > > @Override
> > > protected void configure()
> > > {
> > > // for troubleshooting this issue I have left
> > > this blank, normally other
> > > // bindings would be bound here that
> > > ExternalDataSourcesModule depends on
> > > }
> >
> > > });
> > > Injector childInjector = parentInjector.createChildInjector(new
> > > ExternalDataSourcesModule(), new PushServletModule());
> > > return childInjector;
> > > }
> >
> > > I get the following exception message:
> >
> > > SEVERE: Exception starting filter GuiceFilter
> > > com.google.inject.ConfigurationException: Guice configuration errors:
> >
> > > 1) Unable to create binding for java.util.Map<java.lang.String,
> > > c.w.p.s.ItemService>. It was already configured on one or more child
> > > injectors or private modules
> > > bound at
> > >
> c.w.p.s.ExternalDataSourcesModule.configure(ExternalDataSourcesModule.java:
> > > 53)
> > > If it was in a PrivateModule, did you forget to expose the binding?
> > > while locating java.util.Map<java.lang.String, c.w.p.s.ItemService>
> > > for field at c.w.p.s.ItemResource.itemServices(ItemResource.java:
> > > 23)
> > > while locating c.w.p.s.ItemResource
> >
> > > When I Implement getInjector() like this, I get no errors and
> > > everything works.
> >
> > > @Override
> > > protected Injector getInjector()
> > > {
> > > return Guice.createInjector(new ExternalDataSourcesModule(), new
> > > MyServletModule());
> > > }
> >
> > > No other code has changed other than the getInjector() method.
> > > ExternalDataSourcesModule and MyServletModule are identical in both
> > > implementations of getInjector(). And in the child injector scenario,
> > > the parent injector has not bound anything.
> >
> > > Why do I get the Guice configuration errors when I configure using a
> > > child injector and I don't when I configure with only the main
> > > injector?
> >
> > > Thanks,
> >
> > > Warren Bell
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups "google-guice" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> [email protected].
> > > For more options, visit this group athttp://
> groups.google.com/group/google-guice?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.