使用可注入参数创建 class
Create class with Injectable Parameters
考虑以下 class:
class MongoDumpBuilder (recordingId: String, parquetService: ParquetService) {
...
}
我想注入 parquetService
,问题是我想创建 MongoDumpBuilder
如下:
注意: 我想将 recordingId.stringify
传递给 class
new MongoDumpBuilder(recordingId.stringify)
.withPacificEvents(pacificEvents)
.withAssets(assets)
.withTransactions(transactions)
.withReports(reports)
.withExpenses(expenses)
.build()
但这只适用于:
new MongoDumpBuilder(recordingId.stringify, parquetService)
如果我用 Inject
做 MongoDumpBuilder
我不能创建它
class MongoDumpBuilder @Inject()(recordingId: String, parquetService: ParquetService) {
???
}
有什么解决办法吗?
您可以使用命名来绑定 recordingId
。将此添加到您的 Module
:
bind(classOf[String]).annotatedWith(Names.named("recordingId")).toInstance(recordingId.stringify)
然后定义你的class:
class MongoDumpBuilder @Inject()(@Named("recordingId") val recordingId: String, parquetService: ParquetService) {
...
}
考虑以下 class:
class MongoDumpBuilder (recordingId: String, parquetService: ParquetService) {
...
}
我想注入 parquetService
,问题是我想创建 MongoDumpBuilder
如下:
注意: 我想将 recordingId.stringify
传递给 class
new MongoDumpBuilder(recordingId.stringify)
.withPacificEvents(pacificEvents)
.withAssets(assets)
.withTransactions(transactions)
.withReports(reports)
.withExpenses(expenses)
.build()
但这只适用于:
new MongoDumpBuilder(recordingId.stringify, parquetService)
如果我用 Inject
做 MongoDumpBuilder
我不能创建它
class MongoDumpBuilder @Inject()(recordingId: String, parquetService: ParquetService) {
???
}
有什么解决办法吗?
您可以使用命名来绑定 recordingId
。将此添加到您的 Module
:
bind(classOf[String]).annotatedWith(Names.named("recordingId")).toInstance(recordingId.stringify)
然后定义你的class:
class MongoDumpBuilder @Inject()(@Named("recordingId") val recordingId: String, parquetService: ParquetService) {
...
}