Playframework 如何访问Play Guice模块中的请求?
我正在编写一个处理多个系统的应用程序。用户可以选择要使用的系统,我将该系统ID存储在会话(客户端会话)中 现在我有了服务类,比如CustomerServicePlayframework 如何访问Play Guice模块中的请求?,playframework,request,guice,Playframework,Request,Guice,我正在编写一个处理多个系统的应用程序。用户可以选择要使用的系统,我将该系统ID存储在会话(客户端会话)中 现在我有了服务类,比如CustomerService class CustomerService(val systemID: String) { // Implementation } 我想使用Guice将客户实例注入控制器。但是我想用存储在会话中的SystemID实例化CustomerService 如何访问GUI模块中的request.session 编辑: 我简化了上面的代码
class CustomerService(val systemID: String) {
// Implementation
}
我想使用Guice将客户实例注入控制器。但是我想用存储在会话中的SystemID实例化CustomerService
如何访问GUI模块中的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是一个接口,而不是一个具体的类。无法创建辅助对象工厂
我不想将工厂置于
DefaultCustomerService
下,并在控制器中使用DefaultCustomerService.Factory
。这是因为对于单元测试,我将使用TestCustomerService
存根,并希望依赖项注入将TestCustomerService
注入控制器,而不是DefaultCustomerService
,您不应该这样做。如果需要注入需要运行时值的对象的实例,可以使用guice的
以下是如何在游戏中使用它:
一,。使用运行时值作为参数创建服务工厂:
object CustomerService {
trait Factory {
def apply(val systemID: String): CustomerService
}
}
二,。使用辅助参数实现您的服务
class CustomerService @Inject() (@Assisted systemId: String) { .. }
三,。在GUI模块中绑定工厂:
install(new FactoryModuleBuilder()
.implement(classOf[CustomerService], classOf[CustomerServiceImpl])
.build(classOf[CustomerService.Factory]))
四,。最后,在需要客户服务的工厂注入:
class MyController @Inject() (csFactory: CustomerService.Factory) { .. }
以下是辅助注射的另一个示例:
谢谢rethab-但请参见我在问题中的编辑。FactoryModuleBuilder有一个方法“implement”,在该方法中,您可以将trait绑定到类。我想这就是你要找的。谢谢,我找到了!如果你能在回答中加上这一点,我会接受的。