我有一個依賴於Play的Configuration和WSClient實例的api服務類。無法與Macwire連線播放依賴關係
,我不想使用@Inject()anotation因爲我想使用編譯時注射Macwire因此,我所做的是:
// this is a trait that here im wiring all the dependencies that my api service needs
trait ApiDependencies {
lazy val conf: Configuration = wire[Configuration]
lazy val wsc: WSClient = wire[WSClient]
}
// this is the api service
class ApiService extends ApiDependencies {
def getInfo (id: String): Future[Option[Info]] = {
wsc.url("...").withHttpHeaders(("Content-Type", "application/json")).get.map { response =>
response.status match {
case Status.OK => ...
case Status.NO_CONTENT => ...
case _ => throw new Exception()
}
}
}
}
但我得到一個編譯器錯誤:
Error: Cannot find a value of type: [com.typesafe.config.Config]
lazy val conf: Configuration = wire[Configuration]Error: Cannot find a public constructor nor a companion object for [play.api.libs.ws.WSClient] lazy val wsc: WSClient = wire[WSClient]
有人知道我該如何解決這個問題......?爲什麼發生這種情況:/
謝謝!
可能的重複[如何注入與MacWire服務依賴關係(玩框架)](https://stackoverflow.com/questions/44875361/how-to-inject-dependencies-to-a-service-with- macwire-play-framework) –