【问题标题】:ScalaWS in play framework 2.5 and dependency injection of WSClient in custom classScalaWS in play framework 2.5 和自定义类中 WSClient 的依赖注入
【发布时间】:2016-04-30 06:02:40
【问题描述】:

我正在使用 Scala Play 框架 2.5,我想使用依赖注入将 WSClient 的实例注入到我的自定义类中,但我不断收到以下错误。

not enough arguments for constructor TestClass: (ws: play.api.libs.ws.WSClient)service.TestClass. Unspecified value parameter ws.

运行以下代码时出现错误

class TestClass @Inject() (ws: WSClient) {
  def doSomething() : Future[WSResponse] = {
    ws.url("http://www.google.com").get()
  }
}

val test = new TestClass()
val f = test.doSomething()
val result = Await.result(f, Duration.Inf)

println("doSomething: " + result)

有人可以帮我解决这个尝试将 wsclient 依赖项注入自定义类的问题吗?

提前谢谢你

弗朗西斯

【问题讨论】:

    标签: scala playframework-2.5


    【解决方案1】:

    这一行:

    val test = new TestClass()
    

    它没有使用 Play 提供的依赖注入支持。您正在手动创建TestClass 的实例。我真的建议您阅读以下文档页面:

    PlayFramework: Scala Dependency Injection

    基本上,在使用运行时依赖注入时,您不需要手动创建实例。您让 DI 框架为您完成这项工作。但是,如果您对编译时依赖注入感兴趣,请参阅以下页面:

    PlayFramework: Compile Time Dependency Injection

    【讨论】:

      猜你喜欢
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      相关资源
      最近更新 更多