gpt4 book ai didi

GWT-GIN 多种实现?

转载 作者:行者123 更新时间:2023-12-03 03:36:22 27 4
gpt4 key购买 nike

我有以下代码

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绑定(bind)。但我希望在某些条件下与 ContactDetailViewImplX 绑定(bind)。

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

最佳答案

你不能声明性地告诉 Gin 有时注入(inject)一种实现,有时注入(inject)另一种实现。您可以使用Provider来做到这一点或 @Provides method不过。

提供商示例:

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);
}
}

关于GWT-GIN 多种实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7631153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com