如何在Play Guice模块中访问请求? [英] How can I access request in Play Guice Module?
问题描述
我正在编写一个处理多个系统的应用程序.用户可以选择要使用的系统,然后我将该系统ID存储在会话(客户端会话)中
I am writing an application which handles multiple systems. The user can choose the system which he wants to work with and I store that system ID in session (client session)
现在我有Service类,可以说CustomerService.
Now I have Service classes, lets say CustomerService.
class CustomerService(val systemID: String) {
// Implementation
}
我想使用Guice将Customer实例注入到Controller中.但是我想用存储在会话中的SystemID实例化CustomerService.
I want to use Guice to inject the Customer instance to the Controllers. But I want to instantiate the CustomerService with SystemID which is stored in the session.
如何在Guice模块中访问request.session
?
How can I access request.session
in Guice Module?
在上面简化了我的代码.我的实际代码使用接口.我该如何使用辅助注射?
Had simplified my code above. My actual code uses interfaces. How can I use assisted inject for this?
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工厂.
This gives me: CustomerService is an interface, not a concrete class. Unable to create AssistedInject factory.
而且我不想将Factory放在DefaultCustomerService
下并在控制器中使用DefaultCustomerService.Factory
.这是因为对于单元测试,我将使用TestCustomerService
存根,并且希望依赖注入将TestCustomerService
而不是DefaultCustomerService
注入到控制器中.
And I do not want to put the Factory under DefaultCustomerService
and use DefaultCustomerService.Factory
in the controller. This is because for unit testing I will be using TestCustomerService
stub and want Dependency Injection to inject TestCustomerService
into the controller instead of DefaultCustomerService
.
推荐答案
您不应这样做.如果您需要注入需要运行时值的实例,则可以使用guice的 AssistedInject .
You should not do that. If you need to inject an instance of something that requires runtime-values, you can use guice's AssistedInject.
在游戏中如何使用它:
1.使用运行时值作为参数创建服务的工厂:
1. Create a factory of your service with the runtime value as parameter:
object CustomerService {
trait Factory {
def apply(val systemID: String): CustomerService
}
}
2.使用辅助参数实施服务
2. Implement your service with the assisted parameter
class CustomerService @Inject() (@Assisted systemId: String) { .. }
3.将工厂绑定到您的guice模块中:
3. Bind the factory in your guice module:
install(new FactoryModuleBuilder()
.implement(classOf[CustomerService], classOf[CustomerServiceImpl])
.build(classOf[CustomerService.Factory]))
4.最后将工厂注入您需要客户服务的地方:
4. And finally inject the factory where you need the customer service:
class MyController @Inject() (csFactory: CustomerService.Factory) { .. }
这是辅助注射的另一个示例: https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
Here's another example of assisted inject: https://www.playframework.com/documentation/2.5.x/ScalaTestingWebServiceClients
这篇关于如何在Play Guice模块中访问请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!