本文介绍了注入scala对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Play Framework 2.5,并尝试将WSClient注入控制器中使用的scala对象中.
I'm using Play framework 2.5 and try to inject WSClient in a scala object used in my controllers.
import play.api.libs.concurrent.Execution.Implicits.defaultContext
object MyObject {
@Inject
var ws: WSClient = null
def doSomething() = { // use wsclient }
}
我在多个控制器中使用MyObject,并且在调用doSomething()时wsclient为null.
I use MyObject in several controllers and when calling doSomething() wsclient is null.
推荐答案
您应将MyObject
定义为类,并向其注入wsclient:
You should define MyObject
as class and inject wsclient to it:
class MyObject @Inject()(ws: WSClient) {
def doSomething() = { /* use wsclient */ }
}
这篇关于注入scala对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!