HelloWorld
HelloWorld

Reputation: 95

Guice: A binding was already configured

I have a piece of code with a couple of Modules from different libraries:

install(new AModule())
install(new BModule())

I am getting an error:-

A binding to com.path.to.Class was already configured at com.AModule.provideClass. at com.BModule...

I need both the modules but they cannot be used together because both have bindings to a particular class. I do not own the 2 libraries so cannot make changes there. How can I solve this issue?

Upvotes: 1

Views: 6130

Answers (1)

Olivier Grégoire
Olivier Grégoire

Reputation: 35417

Use Modules.override

If you require the binding present in AModule, use:

install(Modules.override(new BModule()).with(new AModule()));

On the other hand, if you require the binding present in BModule, use:

install(Modules.override(new AModule()).with(new BModule()));

Upvotes: 4

Related Questions