Skip to content

Can an injector inject all implementations of an interface? #303

Answered by eduard-vasinskyi
zzl221000 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @zzl221000

An Injector can inject all implementations of a class, but to do so, it needs to be instructed how to resolve conflicting bindings.

For your case, the easiest way to do this is by instructing Injector to use Set to resolve conflicting bindings.
You can do this by overriding the configure() method of a Module and specifying that you want your bindings to be provided into a set:

bindIntoSet(Key.of(MyFunc.class), Key.of(MyFuncImpl1.class));
bindIntoSet(Key.of(MyFunc.class), Key.of(MyFuncImpl2.class));

The resulting class would look something like this:

public class TestModule extends AbstractModule {

   @Override
   protected void configure() {
      bindIntoSet(Key.of(MyFunc.c…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@zzl221000
Comment options

@eduard-vasinskyi
Comment options

Answer selected by zzl221000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants