简体   繁体   中英

Guice Modules.override on binding specified in multiple modules?

I have two Guice modules that each do an @Provides on the same interface. Each runs independently just fine.

Now I have a 3rd module that wants to bring in both of those. I want that 3rd module's @Provides to trump the others, which seems ideal for Modules.override. Indeed, this works.

Modules.override(childModule1).with(parent);

The parent trumps the child's binding. Great!

This, however, fails :(.

Modules.override(childModule1, childModule2).with(parent);

In that case, it complains that a binding to Foo was "already bound" by the childModule2. Looks like override only works if there's one submodule doing the providing. Is there something I've missed to make this work?

I figured out a solution, though it seems firmly in the hack category. I had to nest the overides.

Modules.override(Modules.override(childModule1).with(childModule2)).with(parent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM