How do I bind Different Interfaces using Google Guice?

后端 未结 3 915
臣服心动
臣服心动 2021-01-14 01:02

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

Chef newChef = Guice.createInjector(Stage.DEVELOPMENT, new Module()          


        
3条回答
  •  情话喂你
    2021-01-14 01:18

    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

提交回复
热议问题