我如何使用GWTP与ginjector扩展



我有一个模块文件:

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

My clientinjectoradd is:

public interface ClientInjectorAdditional extends Ginjector {

    NotificationFetcher getNotificationFetcher();
}

现在我想在我的入口点类中注入NotificationFetcher。I tried

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})公共接口clientinjector扩展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

GWTP会自动创建ginjector和它的所有呈现器和视图获取器。它还支持为非GWTP对象扩展这个ginjector。你可以这样做:

。定义一个接口,让它命名为ginjectoreextensions在package 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",一切都应该工作。

下面是直接来自Gin教程的示例代码:

请找出不符点:

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

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

相关内容

  • 没有找到相关文章

最新更新