3

私の GWT アプリケーションでは、DI メカニズムをセットアップしようとしています。これにより、一般的に必要なものをすべて手元に置くことができます。GWT の guice の適応である google-gin を使用しています。次のように定義されたインジェクターインターフェイスがあります。

@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);、次の例外が発生します。

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 クラスが実際には他の依存関係の 1 つに依存していることです。次のようなプロバイダーを実装しました。

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

これを機能させるには何を変更する必要がありますか?

4

4 に答える 4

2

古い質問ですが、同じ問題があり、ここに落ち続けました。ジンジェクション中にどのクラスが混乱しているかを知る方法をついに見つけました。

アプリを開発モードで起動し、スタックをトレースに配置すると、「新しくコンパイルされたユニットの検証」というステップがあることに気付きました。この下でエラーが発生しましたが、赤色でさえない2つのノードを展開する必要があったため、気づきませんでした。

エラーは でした"No source code available for type com.xxx.xxxx ..."。これは、Javascript に変換できなかったクライアント側の不適切なインポートが原因でした。

これが他のここに役立つことを願っています!

于 2012-02-27T13:59:30.890 に答える
1

また、GIN 1.x を使用して多くの NullPointerException 警告を受け取りましたが、何が起こったのかについての実際の説明はありませんでした。gin 2.0 にアップグレードすると、エラーの内容が正確にわかりました。この質問をした 1 年後にリリースされた 2.0 バージョンにアップグレードすると、問題が解決する場合があります。

于 2013-01-22T10:38:26.843 に答える
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 にスコーピングを処理させます。つまり、「Guice を機能させます」。

それ以外は、あなたの問題はasEagerSingleton. これを justin(Singleton.class)で試すことをお勧めします。また、シングルトンを熱望する必要はなかったと思います。他の人もこの動作に問題を抱えていたようです。いくつかのケースでは、注釈の使いすぎasEagerSingletonや誤解に関係しているという兆候があります。@Singleton

于 2011-09-24T21:26:38.493 に答える
0

同じ問題、同じトレースがあり、エラーは「クライアント」クラスで「サーバー」クラスを使用したため、GIN はこれらのクラスを見つけることができませんでした。

「サーバー」と「クライアント」とは、プロジェクト内のパッケージを意味します。

これが役立つことを願っています

于 2011-12-09T09:04:36.707 に答える