0

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?

Visiedo
  • 377
  • 5
  • 9
  • I would say the problem is that `S2SAuthClientImpl` doesn't have an `@Inject` annotation (or one of its dependencies). – rethab Jul 18 '17 at 10:02
  • It actually does. Here is the definition: `class S2SAuthClientImpl @Inject()(config: S2SAuthServiceConfig, mapper: FinatraObjectMapper) extends S2SAuthClient with Logging ` – Visiedo Jul 19 '17 at 12:24
  • What about it's two dependencies? (etc) – rethab Jul 19 '17 at 13:14
  • The first dependency is provided by my ServiceModule above, it is the S2SAuthServiceConfig. The second is the FinatraObjectMapper, which comes with its own @Provides definition by Twitter. It actually should be the same case with the "Injector" object, also provided by Twitter, but for some reason Guice is complaining. – Visiedo Jul 20 '17 at 09:24

0 Answers0