Guice: Could not find a suitable constructor in com.twitter.inject.Injector

1.1k views Asked by At

I am using Guice injections and Finatra with my service.

When trying to build a small test app I am getting this error:

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)

My Module with the injectors looks like this

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]
  }
}

It works well when I inject these objects in the constructor of my classes, but I get the error when trying to get an object instance like this:

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)
  }

Any ideas why Guice is not able to find the constructor for the Twitter Injector class?

0

There are 0 answers