简体   繁体   English

未满足 Macwire 依赖项

[英]Macwire dependencies not being fulfilled

I am using wire in my scala project.我在我的 scala 项目中使用电线。 I have a usecase ---我有一个用例---

class SchemaRegistry(registry: SchemaRegistryClient) 

class SchemaRegistryClient(url: String) extends RestService(url) {}

trait EndpointModule  {
  // Schema Registry endpoint dependency
  lazy val schemaRegistry: SchemaRegistry = wire[SchemaRegistry]
  def schemaRegistryClient(url: String): SchemaRegistryClient = wire[SchemaRegistryClient]
}

object LightweightinstructionRunner extends EndpointModule with ConfigModule {
    val client = schemaRegistryClient("")
}

This throws an error -这会引发错误 -

Cannot find a value of type: [etl.infrastructure.endpoints.SchemaRegistryClient]找不到类型的值:[etl.infrastructure.endpoints.SchemaRegistryClient]

It works if I create hardcoded object of SchemaRegistryClient inside the EndpointModule .如果我在EndpointModule内创建SchemaRegistryClient的硬编码 object ,它将起作用。

(val c = new SchemaRegsitryClient(""))

Can anyone help in explaining how to workaround this and what is happening here?谁能帮助解释如何解决这个问题以及这里发生了什么?

I cant seem to find a way to fulfill the dependency.我似乎无法找到一种方法来满足依赖。

LightweightinstructionRunner is in a diff package while SchemaRegistry and SchemaRegistryClient are in the same package. LightweightinstructionRunner位于不同的 package 中,而SchemaRegistrySchemaRegistryClient位于相同的 package 中。

The SchemaRegistryClient to be used has to be in scope for the macro to find it.要使用的SchemaRegistryClient必须位于 scope 中,宏才能找到它。 You could declare an abstract def for it in your EndpointModule (without any parameter, because MacWire does not know which url to put in there)您可以在EndpointModule中为其声明一个抽象定义(不带任何参数,因为def不知道将哪个url放在那里)

trait EndpointModule  {
  // Schema Registry endpoint dependency
  def client: SchemaRegistryClient
  lazy val schemaRegistry: SchemaRegistry = wire[SchemaRegistry]
}

object LightweightinstructionRunner extends EndpointModule with ConfigModule {
  override val client = SchemaRegistryClient("")
}

// or maybe

class LightweightinstructionRunner(url: String) extends EndpointModule with ConfigModule {
  override val client = SchemaRegistryClient(url)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM