如何使用 Guice 的 AssistedInject?
我已阅读https://github.com/google/guice/wiki/AssistedInject ,但它没有说明如何传入 AssistedInject 参数的值。 Injector.getInstance() 调用会是什么样子?
I've read https://github.com/google/guice/wiki/AssistedInject, but it doesn't say how to pass in the values of the AssistedInject arguments. What would the injector.getInstance() call look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查 FactoryModuleBuilder 类。
AssistedInject
允许您为类动态配置Factory
,而不是自己编码。当您的对象具有应注入的依赖项以及必须在创建对象期间指定的一些参数时,这通常很有用。文档中的示例是
RealPayment
请参阅
CreditService
和AuthService
应由容器注入,但 startDate 和金额应由开发人员在开发过程中指定实例创建。因此,您不是注入
Payment
,而是注入PaymentFactory
,其参数在RealPayment
中标记为@Assisted
并且应该绑定工厂
配置好的工厂可以注入到您的类中。
并在您的代码中使用
Check the javadoc of FactoryModuleBuilder class.
AssistedInject
allows you to dynamically configureFactory
for class instead of coding it by yourself. This is often useful when you have an object that has a dependencies that should be injected and some parameters that must be specified during creation of object.Example from the documentation is a
RealPayment
See that
CreditService
andAuthService
should be injected by container but startDate and amount should be specified by a developer during the instance creation.So instead of injecting a
Payment
you are injecting aPaymentFactory
with parameters that are marked as@Assisted
inRealPayment
And a factory should be binded
Configured factory can be injected in your classes.
and used in your code