【发布时间】:2016-03-12 16:25:48
【问题描述】:
我正在编写一个处理多个系统的应用程序。用户可以选择他想要使用的系统,我将该系统 ID 存储在会话(客户端会话)中
现在我有了服务类,比如说 CustomerService。
class CustomerService(val systemID: String) {
// Implementation
}
我想使用 Guice 将 Customer 实例注入控制器。但我想用存储在会话中的 SystemID 实例化 CustomerService。
如何在 Guice 模块中访问request.session?
编辑:
已经简化了我上面的代码。我的实际代码使用接口。我该如何使用辅助注射?
trait CustomerService(val systemID: String) {
// Definition
}
object CustomerService{
trait Factory {
def apply(systemID: String) : CustomerService
}
}
class DefaultCustomerService @Inject() (@Assisted systemID: String)
extends CustomerService {
// Definition
}
class CustomerController @Inject()(
val messagesApi: MessagesApi,
csFactory: CustomerService.Factory)
{
}
这给了我: CustomerService 是一个接口,而不是一个具体的类。无法创建 AssistedInject 工厂。
我不想将工厂放在DefaultCustomerService 下并在控制器中使用DefaultCustomerService.Factory。这是因为对于单元测试,我将使用 TestCustomerService 存根并希望依赖注入将 TestCustomerService 注入控制器而不是 DefaultCustomerService。
【问题讨论】:
标签: playframework request guice