GWT-GIN多重实现?

Kan*_*j M 3 gwt gwt-gin dependency-injection

我有以下代码

public class AppGinModule extends AbstractGinModule{
    @Override
    protected void configure() {
        bind(ContactListView.class).to(ContactListViewImpl.class);
        bind(ContactDetailView.class).to(ContactDetailViewImpl.class);
    }
}

@GinModules(AppGinModule.class) 
public interface AppInjector extends Ginjector{
    ContactDetailView getContactDetailView();
    ContactListView getContactListView();
}
Run Code Online (Sandbox Code Playgroud)

在我的切入点

AppInjector appInjector = GWT.create(AppGinModule.class);
appInjector.getContactDetailsView();
Run Code Online (Sandbox Code Playgroud)

ContactDetailView总是绑定ContactsDetailViewImpl.但我希望ContactDetailViewImplX在某些条件下与之结合.

我怎样才能做到这一点?请帮助我.

Dan*_*iel 7

你不能声明地告诉Gin有时会注入一个实现,而有时则注入另一个实现.你可以用一个Provider或一个@Provides方法来做.

Provider 例:

public class MyProvider implements Provider<MyThing> {
    private final UserInfo userInfo;
    private final ThingFactory thingFactory;

    @Inject
    public MyProvider(UserInfo userInfo, ThingFactory thingFactory) {
        this.userInfo = userInfo;
        this.thingFactory = thingFactory;
    }

    public MyThing get() {
        //Return a different implementation for different users
        return thingFactory.getThingFor(userInfo);
    }   
}

public class MyModule extends AbstractGinModule {
  @Override
  protected void configure() {
      //other bindings here...

      bind(MyThing.class).toProvider(MyProvider.class);
  }
}
Run Code Online (Sandbox Code Playgroud)

@Provides 例:

public class MyModule extends AbstractGinModule {
    @Override
    protected void configure() {
        //other bindings here...
    }

    @Provides
    MyThing getMyThing(UserInfo userInfo, ThingFactory thingFactory) {
        //Return a different implementation for different users
        return thingFactory.getThingFor(userInfo);
    }
}
Run Code Online (Sandbox Code Playgroud)