Guice を使用してインスタンス (実際には、モジュール/依存性注入コンテキストのシングルトン/単一インスタンス) を生成したいのですが、マネージド インスタンスの一部をプロキシでラップしています。
この背後にある考え方は、「一度に 1 つずつ」リソースを処理するいくつかのアイテムの周りに同期レイヤーを追加することです。私が思いついた唯一の解決策は、2 つのインジェクターを作成することです。
以下のコードを考えると、
public class ApplicationContext {
private Injector injector;
public <T> T get(Class<? extends T> cls) {
return injector.getInstance(cls);
}
public ApplicationContext() {
injector = Guice.createInjector(new Module() {
binder.bind(InterfaceOne.class).to(ImplementationOne.class);
binder.bind(InterfaceTwo.class).to(ImplementationTwo.class);
binder.bind(InterfaceThree.class).to(ImplementationThree.class);
});
}
}
}
にImplementationThree依存しInterfaceTwo、ImplementationTwo次に依存しInterfaceOneます。
私が今欲しいのは、ImplementationTwoがインスタンス化された後、 に注入される前に Proxy でラップしたいということですImplementationThree。そう:
ImplementationOneGuiceを使って注入してもらいたいImplementationTwo- before
ImplementationTwoが注入されImplementationThree、ラップしたい。
私が見たいのは、依存関係のインスタンス化と注入の後、インジェクターのコンテキストに渡される前に呼び出される Guice インターセプターです。
Providerfor を使用できますが、Guice からImplementationTwoのインスタンスを取得する方法がわかりません。InterfaceOne