GWT-GIN 多种实现? [英] GWT-GIN Multiple Implementations?

查看:38
本文介绍了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 绑定.

Here ContactDetailView is always bind with ContactsDetailViewImpl. But i want that to bind with ContactDetailViewImplX under some conditions.

我该怎么做?请帮助我.

How can i do that? Pls help me.

推荐答案

您不能声明性地告诉 Gin 有时注入一个实现,而在其他时候注入另一个实现.您可以使用 Provider@Provides 方法.

You can't declaratively tell Gin to inject one implementation sometimes and another at other times. You can do it with a Provider or a @Provides method though.

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

这篇关于GWT-GIN 多种实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆