Colin,
I set up GuiceServletContextListener#getInjector() to return a child
injector like this:
protected Injector getInjector()
{
Injector parentInjector = Guice.createInjector(new AbstractModule()
{
@Override
protected void configure()
{
bind(AppSettingsService.class).in(Singleton.class);
}
});
AppSettingsService appSettingsService =
parentInjector.getInstance(AppSettingsService.class);
Injector childInjector = parentInjector.createChildInjector(new
MyModule(appSettingsService), new MyServletModule());
return childInjector;
}
I get the following error:
1) Unable to create binding for java.util.Map<java.lang.String,
...ItemService> annotated with
@com.google.inject.name.Named(value=itemServices). It was already
configured on one or more child injectors or private modules
bound at MyModule.configure(MyModule.java:38)
If it was in a PrivateModule, did you forget to expose the binding?
while locating java.util.Map<java.lang.String, ...ItemService>
annotated with @com.google.inject.name.Named(value=itemServices)
for field at ...ItemResource.itemServices(ItemResource.java:23)
while locating …ItemResource
I can get everything to work when I use the parent Injector and manually
instantiate AppSettingsService, but this does not get my
AppSettingsService bound:
protected Injector getInjector()
{
return Guice.createInjector(new MyModule(new AppSettingsService()),
new MyServletModule());
}
The only changes I have made are in the
GuiceServletContextListener#getInjector() method, the modules have not
changed at all between the two implementations of getInjector().
Thanks,
Warren Bell
On 2/23/12 1:38 PM, Colin Decker wrote:
> It sounds like you may be misunderstanding child injectors. A child
> injector inherits all bindings from its parent injector, but not the other
> way around. Here's an overview of what I think you need to do:
>
> - Create an injector (settingsInjector) with all the bindings required by
> your other modules that need injection. So for example, the bindings
> required for AppSettingsService.
> - Use settingsInjector.getInstance to get instances of the modules that
> need injection.
> - Use settingsInjector.createChildInjector to create a child injector,
> passing it those modules and any others it needs.
> - Return that child injector from ServletModule#getInjector(). It will have
> all of your bindings.
>
> Nothing in this scenario should cause the error you mentioned either, I
> don't think. That sounds like something caused by trying to create a
> binding in a parent injector that already existed in a child injector...
> probably caused by a private module.
>
--
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.