3

AndroidでGuice 3.0を使用してDIを行っています。

私は持っている

public interface APIClient { }

public class DefaultAPIClient implements APIClient { }

私がしたことは、私の MyApplication クラスで Guice をブートストラップして、configure メソッドに 1 つのステートメントを持つモジュールを与えることでした。bind(APIClient.class).to(DefaultAPIClient.class);

Guice の例で指示されたことを実行しました

Injector injector = Guice.createInjector(new APIClientModule());
injector.getInstance(APIClient.class);

これを正しく理解していないかもしれませんが、APIClient を使用する複数のアクティビティに APIClient を注入するにはどうすればよいでしょうか?

私はこれをしましたHomeActivity

public class HomeActivity extends RoboActivity {
    @Inject APIClient client;

    protected void onCreate(Bundle savedInstanceState) {
        client.doSomething();
    }
}

これは機能せず、私にGuice configuration errors: 1) No implementation for com.mycompany.APIClient was bound

したがって、これを機能させることができた唯一の方法は@Inject、HomeActivity の APIClient クライアントから を削除し、それを使用して注入することでしたclient = Guice.createInjector(new APIClientModule()).getInstance(APIClient.class);

これは、APIClient を使用するすべてのアクティビティでこれを行う必要があるということですか? 私は何か間違ったことをしているに違いない。

どんな助けでも素晴らしいでしょう。ありがとう!

4

3 に答える 3

4

Roboguiceメーリングリストでこれに答えてくれたGrouponのMichaelBurtonに感謝します。興味のある方のために、これをプログラムで行う方法を説明します。

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, Modules.override(RoboGuice.newDefaultRoboModule(this)).with(new MyCustomModule()));
    }
}

@Inject private APIClient clientこれで、アクティビティに適切に注入できます。

于 2011-11-27T07:52:16.017 に答える
4

Guice 3.0_noaop で Roboguice 2.0 を使用している場合、追加のカスタム モジュールを定義するには、文字列配列リソース ファイル roboguice_modules を使用します。

ドキュメントから ( Upgradingto20 ):

そのクラスがなくなったので、RoboApplication から継承する必要はなくなりました/不可能になりました。デフォルトの RoboGuice バインディングをオーバーライドしたい場合は、res/values/roboguice.xml の「roboguice_modules」と呼ばれる文字列配列リソースにカスタム モジュール クラス名を指定できます。例えば。

<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <string-array name="roboguice_modules">
        <item>PACKAGE.APIClientModule</item>
    </string-array>
</resources>

したがって、次のようにカスタム モジュールを定義する必要があります。

roboguice_modules:

<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <string-array name="roboguice_modules">
        <item>DONT_KNOW_YOUR_PACKAGE.APIClientModule</item>
    </string-array>
</resources>

もちろん、APIClient と DefaultAPIClient の間のバインディングを使用します。

あとはRoboguiceがやってくれます。

于 2011-11-27T00:56:03.367 に答える
-1

Guice 3.0 の代わりにGuice 2.0 (no aop)を使用する必要があります。

Guice を適切に構成するaddApplicationModules(List<Module> modules)には、アプリケーション クラスのメソッドをオーバーライドし、そこにモジュールを追加する必要があります。

public class MyApplication extends RoboApplication {
  @Override
  protected void addApplicationModules(List<Module> modules) {
    modules.add(new YourModule());
  }

アクティビティでインジェクションを使用するには、RoboActivity のメソッドでsuper.onCreate(savedInstanceState) メンバーがインジェクトされるため、呼び出す必要があります。onCreate

public class HomeActivity extends RoboActivity {
    @Inject APIClient client;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // Call this before use injected members
        client.doSomething();
    }
}
于 2011-11-26T23:55:39.737 に答える