私はhttps://github.com/google/guice/wiki/AssistedInjectと読みましたが、AssistedInject引数の値を渡す方法は記述されていません。 injector.getInstance()呼び出しはどのように見えますか?Guice's AssistedInjectの使い方は?
70
A
答えて
127
FactoryModuleBuilderクラスのjavadocを確認してください。
AssistedInject
は、Factory
を自分でコーディングする代わりに動的に設定することができます。これは、挿入する必要がある依存関係を持つオブジェクトと、オブジェクトの作成時に指定する必要があるいくつかのパラメータを持つ場合に便利です。 docummentaitonから
例RealPayment
public class RealPayment implements Payment {
@Inject
public RealPayment(
CreditService creditService,
AuthService authService,
@Assisted Date startDate,
@Assisted Money amount) {
...
}
}
でCreditService
とAuthService
がコンテナによって注入されるべきではなくたstartDateおよび量は、インスタンスの作成時に開発者が指定する必要があることを見ます。
ので、代わりのあなたはRealPayment
public interface PaymentFactory {
Payment create(Date startDate, Money amount);
}
に@Assisted
としてマークされ、工場出荷時は、設定済みの工場が内に注入することができる
install(new FactoryModuleBuilder()
.implement(Payment.class, RealPayment.class)
.build(PaymentFactory.class));
をバインドされるべきパラメータでPaymentFactory
を注入しているPayment
を注入あなたのクラス。関連
@Inject
PaymentFactory paymentFactory;
し、あなたのコードで使用
Payment payment = paymentFactory.create(today, price);
関連する問題
- 1. テストでAssistedInjectを使用する
- 2. Guiceの、インジェクトTypeLiteral <T> @AssistedInject
- 3. 使い方は
- 4. 使い方は
- 5. - 使い方は?
- 6. 元に戻すマネージャの使い方は?それの使い方?
- 7. sstableloaderの使い方は?
- 8. JXDatePickerの使い方は?
- 9. TailCallsの使い方は?
- 10. setuidの使い方は?
- 11. log4j:SocketAppenderの使い方は?
- 12. Androidファイルシステムの使い方は?
- 13. GWTエミュレーションクラスの使い方は?
- 14. boost :: uniform_on_sphereの使い方は?
- 15. ベッドワークの使い方は?
- 16. フラグメントバックポートの使い方は?
- 17. collect_listの使い方は?
- 18. javascript scrollTimeoutの使い方は?
- 19. json_encodeの使い方は?
- 20. cats typeclassesの使い方は?
- 21. php deployerの使い方は?
- 22. phpbrowserboxの使い方は?
- 23. CSSLintの使い方は?
- 24. lodashフィルターの使い方は?
- 25. StandartErrorの使い方は?
- 26. sympy.physics.quantum Commutatorの使い方は?
- 27. スニペットの使い方は?
- 28. MySQL DECIMALの使い方は?
- 29. Python listの使い方は?
- 30. SGWTEE_HOMEの使い方は?
、以下の答えは、彼と同じタイプを複数回使用されている例をカバーしていません。 https://stackoverflow.com/questions/23553865/using-assisted-inject-with-multiple-params-of-same-type-named-params – Alex