質問

I have a mobule file with:

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

My ClientInjectorAdditional is:

public interface ClientInjectorAdditional extends Ginjector {


    NotificationFetcher getNotificationFetcher();

}

Now I want to inject NotificationFetcher in my entry point class. 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(;
    ...

    }
}

The problem is that notificationFetcher is not injected.

How do I use GWTP with ginjector extensions?

Edit:

When I use

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

I get the following warning:

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

Edit:

I tried:

@GinModules({ ClientModule.class }) public interface ClientInjectorAdditional extends Ginjector { ... }

But this gives the following error:

[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 creates the ginjector automagically with all its presenters and views getters. it also supports extending this ginjector for non GWTP objects. here's how you do it:

a. define an interface, lets name it GinjectorExtensions in package some.package.client

package some.package.client;

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

b. Edit your GWT module xml file to include the following line (which tells GWTP to add your lines of code to it's autogen Ginjector):

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

Then you just @Inject your objects anywhere and everything should work as expected.

EDIT: after reviewing your code, just remove the "extends Ginjector" from ClientInjectorAdditional and everything should work.

他のヒント

Here is the sample code directly from Gin Tutorial:

Please find out the discrepancies:

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

Easier solution is to inject Provider<NotificationFetcher> in constructor and then call provider.get() every time you want to instantiate NotificationFetcher. No additional dependency (like Ginjector, etc.) needs to be defined.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top