Multiple modules, conflicting on @Provides

1,037 views
Skip to first unread message

Chris Kessel

unread,
Mar 22, 2016, 4:58:18 PM3/22/16
to google-guice
I'm adding two Guice modules, but they both have an @Provides for the same item. They're both 3rd party modules, so I can't modify the code. How do I prevent the collision? I really need each module to use it's own provided version. I guess the equivalent of each living within it's own "scope" where it's provided items aren't visible to the other modules.

Guice.createInjector(Stage.DEVELOPMENT, Arrays.asList(moduleA, moduleB));

I've tried wrapping them in a PrivateModule, but then they don't seem to even see their own @Provides...maybe I'm not understanding how to configure a PrivateModule? I'll actually want expose one specific class from each module to to my module that I'm developing, but my first step was trying to get these two other modules to coexist.

The injector supports creation with multiple modules and it seems like provider collision would happen now and then, so presumably there's some way to deal with that I'm not groking...

Thanks,
Chris

Stephan Classen

unread,
Mar 22, 2016, 7:53:28 PM3/22/16
to google...@googlegroups.com
Private modules is the way to go.
A binding in a private module can see all other bindings in the same private module and all bindings of all non private modules.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/26e2e40f-3b26-4ec5-bd20-1c3a2f67fd1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Kessel

unread,
Mar 23, 2016, 1:16:51 PM3/23/16
to google-guice
I must be doing something wrong then. I'm using Dropwizard, which does this behind the scenes:
            injector = injectorFactory.create(this.stage,ImmutableList.copyOf(this.modules));

I'm passing in that list of modules. 

This works:
Arrays.asList(myModule, airlineModule);

This fails:
Arrays.asList(
   myModule, 
   new PrivateModule() {
                    protected void configure() {
                        install(airlinesModule);
                    }
                });

It fails with:

1) Unable to create binding for com.kessel.airlines.AirlineCache. It was already configured on one or more child injectors or private modules
    bound at com.kessel.airlines.AirlineModule.provideAirlineCache() (via modules: com.kessel.MyMain$1 -> com.kessel.airlines.AirlineModule)
  If it was in a PrivateModule, did you forget to expose the binding?

However, the AirlineCache is provided in the AirlineModule. Why is wrapping it in a PrivateModule causing this failure?
    @Provides
    @Singleton
    public AirlineCache provideAirlineCache() {
           ...
     }

Tavian Barnes

unread,
Mar 23, 2016, 9:57:11 PM3/23/16
to google-guice
You have to put both of them in PrivateModules, and export the things you need.

Chris Kessel

unread,
Mar 24, 2016, 10:40:17 AM3/24/16
to google-guice
I'm curious why. "myModule" needs nothing from, and provides nothing to, the airlinesModule. Why would it also need to be wrapped in a PrivateModule? The only reason I'm wrapping airlineModule is because I have a 3rd module I want to add which would have conflicts with airlineModule. My first step was trying to get airlineModule to work as a PrivateModule before adding in the complexity of the 3rd module.

Tavian Barnes

unread,
Mar 24, 2016, 12:43:15 PM3/24/16
to google-guice
Sorry, I misinterpreted your previous message.

Are you sure myModule doesn't require an AirlineCache?  That's the error you get when depending on a private binding from outside the PrivateModule that configured it.  The error message should say what needed an AirlineCache, below what you posted.
Reply all
Reply to author
Forward
0 new messages