【问题标题】:Trouble with EventBus and Gin in Gwt 2.4Gwt 2.4 中的 EventBus 和 Gin 问题
【发布时间】:2012-03-05 06:16:03
【问题描述】:

我正在尝试在 MVP GWT 2.4 中使用 Gin。在我的模块中,我有:

import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;

    @Override
      protected void configure() {
        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        ...
      }

上面的代码使用了新的com.google.web.bindery.event.shared.EventBus。当我想在实现 Activity 的 MVP 活动中注入事件总线时,问题就来了:

package com.google.gwt.activity.shared;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public interface Activity {

  ...

  void start(AcceptsOneWidget panel, EventBus eventBus);
}

Activity 使用已弃用的com.google.gwt.event.shared.EventBus。我怎样才能调和两者?显然,如果我要求使用已弃用的 EventBus 类型,那么 Gin 会抱怨,因为我没有为它指定绑定。

更新:这将允许应用程序构建,但现在有两个不同的EventBuss,这太糟糕了:

 protected void configure() {
    bind(com.google.gwt.event.shared.EventBus.class).to(
        com.google.gwt.event.shared.SimpleEventBus.class).in(Singleton.class);
    bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
    ...

【问题讨论】:

  • 一个技巧是在我的代码中随处使用已弃用的版本。这样做有多糟糕?

标签: java gwt mvp gwt-gin event-bus


【解决方案1】:

为此记录了一个当前问题:http://code.google.com/p/google-web-toolkit/issues/detail?id=6653

不幸的是,建议的解决方法是暂时在您的代码中坚持使用已弃用的EventBus

【讨论】:

    【解决方案2】:

    我问了一个类似的问题:Which GWT EventBus should I use?

    您不需要已弃用的事件总线,因为它扩展了 WebBindery 之一。

    创建一个基础 Activity,您的所有 Activity 都使用此代码扩展:

    // Forward to the web.bindery EventBus instead
    @Override
    @Deprecated
    public void start(AcceptsOneWidget panel, com.google.gwt.event.shared.EventBus eventBus) {
       start(panel, (EventBus)eventBus);
    }
    
    public abstract void start(AcceptsOneWidget panel, EventBus eventBus);
    

    【讨论】:

      【解决方案3】:

      我认为更清洁的解决方案是:

      public class GinClientModule extends AbstractGinModule {
      
          @Override
          protected void configure() {
              bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
              ...
          }
      
          @Provides
          @Singleton
          public com.google.gwt.event.shared.EventBus adjustEventBus(
                  EventBus busBindery) {
              return (com.google.gwt.event.shared.EventBus) busBindery;
          }
      
      ...
      

      请参考我的回答here

      【讨论】:

        【解决方案4】:

        我发现将新旧版本绑定到同一个实例会有所帮助。

            /*
             * bind both versions of EventBus to the same single instance of the
             * SimpleEventBus
             */
            bind(SimpleEventBus.class).in(Singleton.class);
            bind(EventBus.class).to(SimpleEventBus.class);
            bind(com.google.gwt.event.shared.EventBus.class).to(SimpleEventBus.class);
        

        现在,每当您的代码需要 EventBus 时,注入代码所需的 EventBus 并避免弃用警告。

        【讨论】:

          猜你喜欢
          • 2012-04-21
          • 1970-01-01
          • 1970-01-01
          • 2011-11-28
          • 2011-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-13
          相关资源
          最近更新 更多