【问题标题】:GWT-GIN Multiple Implementations?GWT-GIN 多种实现?
【发布时间】:2011-11-29 16:14:48
【问题描述】:

我有以下代码

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 绑定。

我该怎么做?请帮助我。

【问题讨论】:

  • 您希望在什么条件下替换不同的实现?对于不同的浏览器?
  • 没有。这是为了用户权限。两个不同的用户集的两个视图。就像我们使用工厂一样,我们会根据某些条件得到不同的实现。

标签: gwt dependency-injection gwt-gin


【解决方案1】:

您不能以声明方式告诉 Gin 有时注入一个实现,而在其他时候注入另一个实现。不过,您可以使用 Provider@Provides method 来完成。

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

【讨论】:

  • 谢谢。这就是我要搜索的内容。
猜你喜欢
  • 2012-03-13
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多