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

时间:2014-03-22 13:51:49

标签: java javascript gwt gwtp gwt-platform

我有一个mobule文件:

<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

3 个答案:

答案 0 :(得分:1)

GWTP使用其所有演示者和视图getter自动创建ginjector。 它还支持为非GWTP对象扩展此ginjector。 这是你如何做到的:

一个。定义一个接口,让我们在包some.package.client

中命名为GinjectorExtensions
package some.package.client;

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

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

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

然后你只需@Inject你的对象,一切都应该按预期工作。

编辑:在审核完代码后,只需从ClientInjectorAdditional中删除“extends Ginjector”,一切都应该有效。

答案 1 :(得分:0)

以下是直接来自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);
    
    }
    

答案 2 :(得分:0)

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