GWT-GIN Несколько реализаций?
У меня есть следующий код
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();
}
В моей точке входа
AppInjector appInjector = GWT.create(AppGinModule.class);
appInjector.getContactDetailsView();
Вот ContactDetailView
всегда связаны с ContactsDetailViewImpl
, Но я хочу, чтобы это было связано с ContactDetailViewImplX
при некоторых условиях.
Как я могу это сделать? Пожалуйста, помогите мне.
1 ответ
Решение
Вы не можете декларативно сказать Джину вводить одну реализацию иногда, а другую - в другое. Вы можете сделать это с 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);
}
}
@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);
}
}