【问题标题】:How do I use GWTP with ginjector extensions?如何将 GWTP 与 ginjector 扩展一起使用?
【发布时间】:2014-03-22 13:51:48
【问题描述】:

我有一个移动文件:

<extend-configuration-property name="gin.ginjector.extensions" value="test.client.gin.ClientInjectorAdditional"/>

我的 ClientInjectorAdditional 是:

public interface ClientInjectorAdditional extends Ginjector {


    NotificationFetcher getNotificationFetcher();

}

现在我想在我的入口点类中注入 NotificationFetcher。我试过了

public class Test implements EntryPoint {

    private static final ApplicationController controller = GWT.create(ApplicationController.class);

    @Inject
    private NotificationFetcher notificationFetcher;

    @Override

    public void onModuleLoad() {
        controller.init(;
    ...

    }
}

问题是notificationFetcher 没有被注入。

如何将 GWTP 与 ginjector 扩展一起使用?

编辑:

当我使用时

 private final ClientInjectorAdditional injector = GWT.create(ClientInjectorAdditional.class);

我收到以下警告:

  No gin modules are annotated on Ginjector interface test.client.gin.ClientInjectorAdditional, did you forget the @GinModules annotation?

编辑:

我试过了:

@GinModules({ ClientModule.class }) 公共接口 ClientInjectorAdditional 扩展 Ginjector { ... }

但这会产生以下错误:

[DEBUG] [test] - Rebinding test.client.gin.ClientInjectorAdditional
    [DEBUG] [test] - Invoking generator com.google.gwt.inject.rebind.GinjectorGenerator
        [ERROR] [test] - Error injecting com.gwtplatform.dispatch.rest.client.ActionMetadataProvider: Unable to create or inherit binding: No @Inject or default constructor found for com.gwtplatform.dispatch.rest.client.ActionMetadataProvider
  Path to required node:

com.gwtplatform.dispatch.rest.client.RestRequestBuilderFactory [com.gwtplatform.dispatch.rest.client.gin.RestDispatchAsyncModule.configureDispatch(RestDispatchAsyncModule.java:99)]
 -> com.gwtplatform.dispatch.rest.client.DefaultRestRequestBuilderFactory [com.gwtplatform.dispatch.rest.client.gin.RestDispatchAsyncModule.configureDispatch(RestDispatchAsyncModule.java:99)]
 -> com.gwtplatform.dispatch.rest.client.ActionMetadataProvider [@Inject constructor of com.gwtplatform.dispatch.rest.client.DefaultRestRequestBuilderFactory]

        [ERROR] [test] - Error injecting com.gwtplatform.dispatch.rest.client.serialization.JacksonMapperProvider: Unable to create or inherit binding: No @Inject or default constructor found for com.gwtplatform.dispatch.rest.client.serialization.JacksonMapperProvider
  Path to required node:

com.gwtplatform.dispatch.rest.client.serialization.Serialization [com.gwtplatform.dispatch.rest.client.gin.RestDispatchAsyncModule.configureDispatch(RestDispatchAsyncModule.java:103)]
 -> com.gwtplatform.dispatch.rest.client.serialization.JsonSerialization [com.gwtplatform.dispatch.rest.client.gin.RestDispatchAsyncModule.configureDispatch(RestDispatchAsyncModule.java:103)]
 -> com.gwtplatform.dispatch.rest.client.serialization.JacksonMapperProvider [@Inject constructor of com.gwtplatform.dispatch.rest.client.serialization.JsonSerialization]

[ERROR] [test] - Deferred binding failed for 'test.client.gin.ClientInjectorAdditional'; expect subsequent failures
[ERROR] [test] - Failed to create an instance of 'test.client.test' via deferred binding 
[ERROR] [test] - Unable to load module entry point class test.client.test (see associated exception for details)
[ERROR] [test] - Failed to load module 'test' from user agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36' at http-server.fritz.box:61196

【问题讨论】:

  • 尝试分析异常。读它在说什么?并按照我的帖子中提到的说明进行操作。我没有使用过@Inject。 No default constructor found for com.gwtplatform.dispatch.rest.client.ActionMetadataProviderNo default constructor found for com.gwtplatform.dispatch.rest.client.serialization.JacksonMapperProvider
  • 从 gwt.xml 中删除 &lt;extend-configuration-property name="gin.ginjector.extensions"...。使用GWT.create()
  • 在这种情况下我必须使用
  • 首先尝试按照我的帖子中提到的步骤进行操作。让我知道它是否不起作用。除非我有完整的代码,否则我不能说。使用GWT.create() 代替@Inject
  • 您是否正在使用 gwtp 并为您自动生成 gingector?

标签: java javascript gwt gwtp gwt-platform


【解决方案1】:

GWTP 使用它的所有演示者和视图获取器自动创建 ginjector。 它还支持为非 GWTP 对象扩展这个 ginjector。 这是你的做法:

一个。定义一个接口,我们将其命名为 GinjectorExtensions 包中的 some.package.client

package some.package.client;

public interface GinjectorExtensions {
    //your objects here
    MyConstants getMyConstants();  
    MyMessages MyMessages();
    MyRequestFactory getRequestFactory(); 
}

b.编辑您的 GWT 模块 xml 文件以包含以下行(告诉 GWTP 将您的代码行添加到它的 autogen Ginjector):

   <set-configuration-property name="gin.ginjector.extensions"
                              value="some.package.client.GinjectorExtensions"/> 

然后你只需在任何地方@Inject 你的对象,一切都会按预期工作。

编辑:查看您的代码后,只需从 ClientInjectorAdditional 中删除“extends Ginjector”即可,一切正常。

【讨论】:

  • 如何访问 GinjectorExtensions 中的 get 方法?
【解决方案2】:

这是直接来自Gin Tutorial的示例代码:

请找出差异:

  • gwt.xml

      <inherits name="com.google.gwt.inject.Inject"/>
    
  • MyWidgetClientModule.java

    import com.google.gwt.inject.client.AbstractGinModule;
    import com.google.inject.Singleton;
    
    public class MyWidgetClientModule extends AbstractGinModule {
        protected void configure() {
            bind(MyWidgetMainPanel.class).in(Singleton.class);
        }
    }
    
  • MyWidgetGinjector.java

    import com.google.gwt.inject.client.GinModules;
    import com.google.gwt.inject.client.Ginjector;
    
    @GinModules(MyWidgetClientModule.class)
    public interface MyWidgetGinjector extends Ginjector {
        MyWidgetMainPanel getMainPanel();
    }
    
  • MyWidgetMainPanel.java

    import com.google.gwt.user.client.ui.Button;
    
    public class MyWidgetMainPanel extends Button {
    
    }
    
  • EntryPoint.java

    private final MyWidgetGinjector injector = GWT.create(MyWidgetGinjector.class);
    
    public void onModuleLoad() {
    
        MyWidgetMainPanel mainPanel = injector.getMainPanel();
        mainPanel.setText("Hi");
        RootPanel.get().add(mainPanel);
    
    }
    

【讨论】:

    【解决方案3】:

    更简单的解决方案是在构造函数中注入Provider&lt;NotificationFetcher&gt;,然后每次要实例化NotificationFetcher 时调用provider.get()。无需定义额外的依赖项(如 Ginjector 等)。

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 1970-01-01
      • 2011-04-28
      • 2020-12-01
      • 2020-02-27
      • 1970-01-01
      • 2015-08-30
      • 2019-01-17
      • 2014-10-21
      相关资源
      最近更新 更多