mardi 31 octobre 2017

Scala, classTag and guice: Error injecting constructor, java.lang.StackOverflowError

In my play 2 application, I need to create a class tag in the class which is instantiated by guice (as eager singleton, this is requirement).

case class UserCreated(id: Long, userName: String) extends ApplicationEvent

@Singleton
class AccountRoleUpdater @Inject()
    (accountRepository: AccountRepository)
    (implicit val actorSystem: ActorSystem, ec: ExecutionContext) {

    implicit def ct: ClassTag[UserCreated] = classTag[UserCreated]

    // THIS LINE CAUSES ERROR       
    val cls = ct.runtimeClass.asInstanceOf[Class[UserCreated]]     
}

Here is guice bindning:

class Module extends AbstractModule with AkkaGuiceSupport {
    bind(classOf[account.eventlistener.AccountRoleUpdater]).asEagerSingleton()
}

When application starts, I'm getting exception:

CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.StackOverflowError at account.eventlistener.AccountRoleUpdater.(AccountRoleUpdater.scala:45) at Module.configure(Module.scala:64) (via modules: com.google.inject.util.Modules$OverrideModule -> Module) while locating account.eventlistener.AccountRoleUpdater

1 error] at play.core.server.DevServerStart$$anon$1.reload(DevServerStart.scala:186) at play.core.server.DevServerStart$$anon$1.get(DevServerStart.scala:124) at play.core.server.AkkaHttpServer.modelConversion(AkkaHttpServer.scala:183) at play.core.server.AkkaHttpServer.handleRequest(AkkaHttpServer.scala:189) at play.core.server.AkkaHttpServer.$anonfun$createServerBinding$3(AkkaHttpServer.scala:106) at akka.stream.impl.fusing.MapAsync$$anon$23.onPush(Ops.scala:1172) at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:499) at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:462) at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:368) at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:571) Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.StackOverflowError at account.eventlistener.AccountRoleUpdater.(AccountRoleUpdater.scala:45) at Module.configure(Module.scala:64) (via modules: com.google.inject.util.Modules$OverrideModule -> Module) while locating account.eventlistener.AccountRoleUpdater

1 error at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:470) at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:184) at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110) at com.google.inject.Guice.createInjector(Guice.java:99) at com.google.inject.Guice.createInjector(Guice.java:84) at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:185) at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137) at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21) at play.core.server.DevServerStart$$anon$1.$anonfun$reload$3(DevServerStart.scala:174) at play.utils.Threads$.withContextClassLoader(Threads.scala:21) Caused by: java.lang.StackOverflowError: null at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21) at account.eventlistener.AccountRoleUpdater.ct(AccountRoleUpdater.scala:21)

Seems some cycle reference...

Plese help!





Aucun commentaire:

Enregistrer un commentaire