2011-09-23 64 views
3

在我的GWT應用程序中,我試圖建立一個DI機制,它將使我能夠隨時隨地獲得所有常見必需的東西。我正在使用google-gin,它是GWT的一個改編版本。我有一個注射器接口定義是這樣的:谷歌杜松子酒提供者需要依賴。 NullPointerException BindingsProcessor.java:498

@GinModules(InjectionClientModule.class) 
public interface MyInjector extends Ginjector { 
    public PlaceController getPlaceController(); 
    public Header getHeader(); 
    public Footer getFooter(); 
    public ContentPanel getContent(); 
    public EventBus getEventBus(); 
    public PlaceHistoryHandler getPlaceHistoryHandler(); 
} 

我注射模塊是這樣的:

public class InjectionClientModule extends AbstractGinModule { 
    public InjectionClientModule() { 
     super(); 
    } 

    protected void configure() { 
     bind(Header.class).in(Singleton.class); 
     bind(Footer.class).in(Singleton.class); 
     bind(ContentPanel.class).in(Singleton.class); 
     bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class); 
     bind(PlaceController.class).toProvider(PlaceControllerProvider.class).asEagerSingleton(); 
     bind(PlaceHistoryHandler.class).toProvider(PlaceHistoryHandlerProvider.class).asEagerSingleton(); 
    } 
} 

當調用MyInjector injector = GWT.create(MyInjector.class);我正在gettign以下異常:

java.lang.NullPointerException: null 
at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBinding(BindingsProcessor.java:498) 
at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBindingForUnresolved(BindingsProcessor.java:290) 
at com.google.gwt.inject.rebind.BindingsProcessor.createImplicitBindingsForUnresolved(BindingsProcessor.java:278) 
at com.google.gwt.inject.rebind.BindingsProcessor.process(BindingsProcessor.java:240) 
at com.google.gwt.inject.rebind.GinjectorGeneratorImpl.generate(GinjectorGeneratorImpl.java:76) 
at com.google.gwt.inject.rebind.GinjectorGenerator.generate(GinjectorGenerator.java:47) 
at com.google.gwt.core.ext.GeneratorExtWrapper.generate(GeneratorExtWrapper.java:48) 
at com.google.gwt.core.ext.GeneratorExtWrapper.generateIncrementally(GeneratorExtWrapper.java:60) 
at com.google.gwt.dev.javac.StandardGeneratorContext.runGeneratorIncrementally(StandardGeneratorContext.java:647) 
at com.google.gwt.dev.cfg.RuleGenerateWith.realize(RuleGenerateWith.java:41) 
at com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.rebind(StandardRebindOracle.java:78) 
at com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:268) 
at com.google.gwt.dev.shell.ShellModuleSpaceHost.rebind(ShellModuleSpaceHost.java:141) 
at com.google.gwt.dev.shell.ModuleSpace.rebind(ModuleSpace.java:585) 
at com.google.gwt.dev.shell.ModuleSpace.rebindAndCreate(ModuleSpace.java:455) 
at com.google.gwt.dev.shell.GWTBridgeImpl.create(GWTBridgeImpl.java:49) 
at com.google.gwt.core.client.GWT.create(GWT.java:97) 

的問題是PlaceController類實際上依賴於其他依賴項之一。我實現了它的供應商是這樣的:

public class PlaceControllerProvider implements Provider<PlaceController> { 
    private final PlaceController placeController; 

    @Inject 
    public PlaceControllerProvider(EventBus eventBus) { 
     this.placeController = new PlaceController(eventBus); 
    } 

    @Override 
    public PlaceController get() { 
     return placeController; 
    } 
} 

我應該改變這個工作?

回答

1

雖然我沒有真正看到你得到的錯誤是如何與被注入的PlaceController相關的,但我確實看到提供者正在返回一個單身PlaceController,即使提供者沒有被綁定爲一個單身人或者不同的範圍。編寫該提供程序的正確方法是:

public class PlaceControllerProvider implements Provider<PlaceController> { 
    private final EventBus eventBus; 

    @Inject 
    public PlaceControllerProvider(EventBus eventBus) { 
     this.eventBus = eventBus; 
    } 

    @Override 
    public PlaceController get() { 
     return new PlaceController(eventBus); 
    } 
} 

讓guice處理範圍即「Letting guice for you」。

除此之外,我幾乎打賭,你的問題是由於使用asEagerSingleton。我建議你只用in(Singleton.class)來試試這個,我進一步假設你並不真的需要這個單身男人。似乎也有others had problems的行爲,但有一些跡象表明它在某些情況下過度使用asEagerSingleton或誤解@Singleton註釋。

0

有同樣的問題,同樣的痕跡,錯誤是我在我的「客戶」類中使用「服務器」類,所以GIN找不到這些類。

我的意思是「服務器」和「客戶端」我的項目中的軟件包。

希望能幫助

2

老問題,但有我一直在這裏落下了同樣的問題。我終於找到了在ginjection期間知道哪個班級正在搞亂的方法。

當我在開發模式下啓動我的應用程序並將堆棧放入Trace時,我發現有一個步驟叫做「驗證新編譯的單元」。 在此之下,我有一個錯誤,但我沒有注意到它,因爲我不得不擴大2個甚至不是紅色的節點。

錯誤是"No source code available for type com.xxx.xxxx ...",這是由於客戶端導入不良,導致無法轉換爲Javascript。

希望這可以幫助其他在這裏!

1

我也得到了很多NullPointerException警告使用GIN 1.x沒有真正解釋發生了什麼。當我升級到杜松子酒2.0時,我很高興地被告知錯誤是什麼。您可以通過升級到在您提出此問題一年後發佈的2.0版本來獲得幫助。