Guice:在com.twitter.inject.Injector

时间:2017-07-18 08:51:12

标签: scala guice code-injection finatra

我正在使用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类的构造函数的任何想法?

0 个答案:

没有答案