首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GWT-GIN多个实现?

GWT-GIN多个实现?
EN

Stack Overflow用户
提问于 2011-10-03 12:56:37
回答 1查看 2.2K关注 0票数 3

我有以下代码

代码语言:javascript
复制
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();
}

在我的入口点

代码语言:javascript
复制
AppInjector appInjector = GWT.create(AppGinModule.class);
appInjector.getContactDetailsView();

这里,ContactDetailView总是与ContactsDetailViewImpl绑定在一起。但在某些情况下,我希望它与ContactDetailViewImplX绑定。

我该怎么做呢?请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-03 16:50:11

你不能声明性地告诉Gin时而注入一个实现,时而又注入另一个。不过,您可以使用Provider@Provides method来完成此操作。

Provider示例:

代码语言:javascript
复制
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示例:

代码语言:javascript
复制
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);
    }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7631153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档