使用MacWire特性向scala对象注入playFramework依赖项失败

使用MacWire特性向scala对象注入playFramework依赖项失败,scala,dependency-injection,playframework,playframework-2.0,macwire,Scala,Dependency Injection,Playframework,Playframework 2.0,Macwire,假设我的项目中有很多汽车对象,例如: object Porsche extends Car { override def start() {...} override def canStart(fuelInLitr: Int) = fuelInLitr > 5 override val fuelInLitr = 45 override val carId = 1234567 } 我正在扩展汽车,这只是设置汽车结构的一个特点: trait Car { def

假设我的项目中有很多汽车对象,例如:

object Porsche extends Car {
   override def start() {...}
   override def canStart(fuelInLitr: Int) = fuelInLitr > 5

   override val fuelInLitr = 45
   override val carId = 1234567
}
我正在扩展汽车,这只是设置汽车结构的一个特点:

trait Car {
  def start(): Unit
  val canStart(fuel: Double): Boolean
  val fuelInLitr: Int
  val carId: Int
}
现在,在
start()

所以我有了这个
CarApiService

class CarApiService (wsClient: WSClient, configuration: Configuration) {

  implicit val formats: Formats = DefaultFormats

  def getCarkey(carId: String): Future[Option[CarKey]] = {

    val carInfoServiceApi = s"${configuration.get[String]("carsdb.carsInfo")}?carId=$carId"

    wsClient.url(carInfoServiceApi).withHttpHeaders(("Content-Type", "application/json")).get.map { response =>
      response.status match {
        case Status.OK => Some(parse(response.body).extract[CarKey])
        case Status.NO_CONTENT => None
        case _ => throw new Exception(s"carsdb failed to perform operation with status: ${response.status}, and body: ${response.body}")
      }
    }
  }
}
我希望能够在我的汽车对象中使用
getCarkey()
,因此我创建了一个CarsApiServicesModule,它将允许我访问
carapise服务
,我可以使用它的方法:

trait CarsApiServicesModule {

  /// this supply the carApiService its confuguration dependancy 
  lazy val configuration: Config = ConfigFactory.load()
  lazy val conf: Configuration = wire[Configuration]

  /// this supply the carApiService its WSClient dependancy 
  lazy val wsc: WSClient = wire[WSClient]

  lazy val carApiService: CarApiService = wire[CarApiService]
}
现在我想用这种方式在我的汽车对象中添加这个特性:

object Porsche extends Car with CarsApiServicesModule {
    // here I want to use myApiService
    // for example: carApiService.getCarkey(carId)...
}
但在编译此文件时,我遇到以下错误:

有人知道问题出在哪里吗


另外,这种设计有意义吗?

您需要记住,
wire
只是一个帮助宏,它试图生成新的实例创建代码:事实上,它非常愚蠢。在这里,它将尝试创建
WSClient
的新实例

但是,并非所有对象都可以使用简单的
new
调用实例化-有时需要调用“factory”方法

在本例中,如果查看,您将看到要实例化
WSClient
,需要通过
StandaloneAhcWSClient()
对象创建它

因此,在这种情况下,
wire
对您没有帮助-您只需手工编写初始化代码。幸运的是它不是太大