0

我在我的服务中使用 Guice 注射和 Finatra。

在尝试构建一个小型测试应用程序时,我收到此错误:

Could not find a suitable constructor in com.twitter.inject.Injector. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. 
    at com.twitter.inject.Injector.class(Injector.scala:9)

我的带有注射器的模块看起来像这样

object ServiceModule extends TwitterModule {

  @Provides
  @Singleton
  def provideS2SAuthServiceConfig(): S2SAuthServiceConfig = {
    val servicePath = DynamicProperty.getInstance("myorg.module.auth.servicePath").getString
    val serviceUrl = DynamicProperty.getInstance("myorg.module.auth.serviceUrl").getString

    val httpClient: Service[Request, Response] = Http.client.withTls(serviceUrl).newService(serviceUrl)

    S2SAuthServiceConfig(httpClient, servicePath)
  }

  @Provides
  @Singleton
  def provideS2SAuthClient(injector: Injector): S2SAuthClient = {
    val s2sAuthClientClass = DynamicProperty.getInstance("myorg.mymodule.s2s.s2sAuthClient").getString
    val s2sAuthClientInstance = injector.instance(Class.forName(s2sAuthClientClass))

    s2sAuthClientInstance.asInstanceOf[S2SAuthClient]
  }
}

当我在类的构造函数中注入这些对象时,它运行良好,但是在尝试获取这样的对象实例时出现错误:

def main (args: Array[String]): Unit = {
    val injector = new Injector(Guice.createInjector(ServiceModule))
    val authClient = injector.instance[S2SAuthClientImpl](classOf[S2SAuthClientImpl])
    val token = authClient.getToken("MyClientID", "MySecret", "MyScope")
    println(token)
  }

任何想法为什么 Guice 无法找到 Twitter Injector 类的构造函数?

4

0 回答 0