5

Suppose I have two modules ModuleA and ModuleB which are self-sufficient and can be used as stand-alone modules. Both the modules have a dependency on a third module ModuleC like

install(new ModuleC());

Now, I have a use case where I need to create an injector with both modules, A and B. I did this:

Guice.createInjector(new ModuleA(), new ModuleB());

It threw a CreationException as expected which said that a binding to a certain class was already configured at one of the modules. Keeping in mind that I do not have the power to alter ModuleA and ModuleB, how do I make it work?

I tried using Modules.combine(Modules... modules) but that did not solve my problem. Is there any solution out there for this?

0

2 Answers 2

10

Consider you have the following bindings defined:

ModuleC:

  • C1

ModuleA

  • A1
  • A2

ModuleB

  • B1
  • B2

Now when you do Guice.createInjector(new ModuleA(),new ModuleB()) or Modules.combine(..), your final list of bindings will be:

  • A1
  • A2
  • B1
  • B2
  • C1 (as inherited from A)
  • C1 (!) (as inherited from B)

as C1 binding is listed twice, that creates a conflict and results in CreationException.

But if you use Modules.override() instead:

Returns a builder that creates a module that overlays override modules over the given modules. If a key is bound in both sets of modules, only the binding from the override modules is kept.

E.g. by doing

Modules.override(new ModuleA()).with(new ModuleB())

Your final list of bindings will be as follows:

  • A1
  • A2
  • B1
  • B2
  • C1 (as inherited from A)
  • C1 (as inherited from B)

The C1 binding inherited from ModuleA will be dropped in favor of the C1 binding as defined in ModuleB, thus resolving the conflict.

3
  • I tried Guice.createInjector(Modules.override(new ModuleA(), new ModuleB()).with(new ModuleC()));
    – rrrocky
    Commented Dec 6, 2016 at 4:39
  • This worked. Guice.createInjector(Modules.override(new ModuleA()).with(new ModuleB()). Thanks. But can you elaborate your answer a bit?
    – rrrocky
    Commented Dec 6, 2016 at 8:59
  • Downvoted because Modules.override() says "Prefer to write smaller modules that can be reused and tested without overrides."
    – MageWind
    Commented Sep 23, 2022 at 16:00
-1

The documentation for Modules.override() says "Prefer to write smaller modules that can be reused and tested without overrides." For production code, here are a few alternatives:

  • Refactor ModuleA and ModuleB to not install ModuleC and install ModuleC when you create the injector. This makes it more clear that bindings in ModuleC are shared between modules A and B.
  • Make ModuleA and ModuleB each a PrivateModule so that the ModuleC bindings are not shared and, therefore, cannot conflict. The potential downside is that now ModuleA and ModuleB will not share bindings (e.g. could be problematic for Singletons and Multibinders).
  • Annotate the bindings in ModuleA &or ModuleB so that they do not share bindings. Then, when they install ModuleC, they tell ModuleC (through its constructor) what the binding annotation should be. This option would be more suitable if you think the bindings for ModuleA and ModuleB could diverge in the future or if the bindings must not be shared. You could also make ModuleC a PrivateModule so that it only needs to annotate the bindings it exposes.
1
  • The poster specifically said he can not alter the modules. Commented Feb 29 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.