Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind Different Interfaces using Google Guice?

Do I need to create a new module with the Interface bound to a different implementation?

Chef newChef = Guice.createInjector(Stage.DEVELOPMENT, new Module() {
     @Override
      public void configure(Binder binder) {
        binder.bind(FortuneService.class).to(FortuneServiceImpl.class);
      }

    }).getInstance(Chef.class);

Chef newChef2 = Guice.createInjector(Stage.DEVELOPMENT, new Module() {
  @Override
  public void configure(Binder binder) {
    binder.bind(FortuneService.class).to(FortuneServiceImpl2.class);
  }

}).getInstance(Chef.class);

I cannot touch the Chef Class nor the Interfaces. I am just a client binding to Chef's FortuneService to different Interfaces at runtime.

like image 518
unj2 Avatar asked Dec 22 '22 03:12

unj2


2 Answers

Take looks like the Robot Legs section, described in the Guice FAQ. "How to create a robot with a two Leg objects, the left one injected with a LeftFoot, and the right one with a RightFoot." But only one Leg class that's reused in both contexts.

There's a PrivateModules solution. It uses two separate private modules, a @Left one and an @Right one. Each has a binding for the unannotated Foot.class and Leg.class, and exposes a binding for the annotated Leg.class:

class LegModule extends PrivateModule {
  private final Class<? extends Annotation> annotation;

  LegModule(Class<? extends Annotation> annotation) {
    this.annotation = annotation;
  }

  @Override protected void configure() {
    bind(Leg.class).annotatedWith(annotation).to(Leg.class);
    expose(Leg.class).annotatedWith(annotation);

    bindFoot();
  }

  abstract void bindFoot();
}

...and to glue it all together:

  public static void main(String[] args) {
    Injector injector = Guice.createInjector(
        new LegModule(Left.class) {
          @Override void bindFoot() {
            bind(Foot.class).toInstance(new Foot("leftie"));
          }
        },
        new LegModule(Right.class) {
          @Override void bindFoot() {
            bind(Foot.class).toInstance(new Foot("righty"));
          }
        });
  }
like image 155
Jesse Wilson Avatar answered Jan 05 '23 19:01

Jesse Wilson


How do you decide which FortuneService implementation is required for Chef?You can not bind the same interface to different implementations without a way for Guice to differentiate between the two. You have to use something like this.

bind(FortuneService.class).annotatedWith(Names.named("1").to(FortuneServiceImpl.class);
bind(FortuneService.class).annotatedWith(Names.named("2").to(FortuneServiceImpl2.class);

For more information, see here

like image 39
Kartik Avatar answered Jan 05 '23 19:01

Kartik