如何访问 Play Guice 模块中的请求?
How can I access request in Play Guice Module?
我正在编写一个处理多个系统的应用程序。用户可以选择他想要使用的系统,我将该系统 ID 存储在会话(客户端会话)
现在我有服务 classes,假设是 CustomerService。
class CustomerService(val systemID: String) {
// Implementation
}
我想使用 Guice 将客户实例注入控制器。但我想用存储在会话中的 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 是一个接口,而不是具体的 class。无法创建 AssistedInject 工厂。
而且我不想将工厂放在 DefaultCustomerService
下并在控制器中使用 DefaultCustomerService.Factory
。这是因为对于单元测试,我将使用 TestCustomerService
存根并希望依赖注入将 TestCustomerService
注入控制器而不是 DefaultCustomerService
.
你不应该那样做。如果你需要注入一个需要运行时值的实例,你可以使用 guice 的 AssistedInject.
以下是如何在游戏中使用它:
1。使用运行时值作为参数创建服务工厂:
object CustomerService {
trait Factory {
def apply(val systemID: String): CustomerService
}
}
2。使用辅助参数实施您的服务
class CustomerService @Inject() (@Assisted systemId: String) { .. }
3。在你的 guice 模块中绑定工厂:
install(new FactoryModuleBuilder()
.implement(classOf[CustomerService], classOf[CustomerServiceImpl])
.build(classOf[CustomerService.Factory]))
4。最后注入需要客服的工厂:
class MyController @Inject() (csFactory: CustomerService.Factory) { .. }
这是辅助注射的另一个例子:
https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
我正在编写一个处理多个系统的应用程序。用户可以选择他想要使用的系统,我将该系统 ID 存储在会话(客户端会话)
现在我有服务 classes,假设是 CustomerService。
class CustomerService(val systemID: String) {
// Implementation
}
我想使用 Guice 将客户实例注入控制器。但我想用存储在会话中的 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 是一个接口,而不是具体的 class。无法创建 AssistedInject 工厂。
而且我不想将工厂放在 DefaultCustomerService
下并在控制器中使用 DefaultCustomerService.Factory
。这是因为对于单元测试,我将使用 TestCustomerService
存根并希望依赖注入将 TestCustomerService
注入控制器而不是 DefaultCustomerService
.
你不应该那样做。如果你需要注入一个需要运行时值的实例,你可以使用 guice 的 AssistedInject.
以下是如何在游戏中使用它:
1。使用运行时值作为参数创建服务工厂:
object CustomerService {
trait Factory {
def apply(val systemID: String): CustomerService
}
}
2。使用辅助参数实施您的服务
class CustomerService @Inject() (@Assisted systemId: String) { .. }
3。在你的 guice 模块中绑定工厂:
install(new FactoryModuleBuilder()
.implement(classOf[CustomerService], classOf[CustomerServiceImpl])
.build(classOf[CustomerService.Factory]))
4。最后注入需要客服的工厂:
class MyController @Inject() (csFactory: CustomerService.Factory) { .. }
这是辅助注射的另一个例子: https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients