无法注入actorSystem服务

时间:2016-11-24 20:52:28

标签: scala playframework

我正在尝试创建一个在我的应用程序后台运行的服务(读取和写入队列),我想用actor系统构建它。但是,当我尝试将ActorSystem注入我的类时,我收到错误:

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.IllegalArgumentException: no matching constructor found on class services.Indexer$IndexActor for arguments []
  at services.Indexer.<init>(Indexer.scala:21)
  at Module.configure(Module.scala:6) (via modules: com.google.inject.util.Modules$OverrideModule -> Module)
  while locating services.Indexer

这是我的设置:

// Module.scala
class Module extends AbstractModule {
    override def configure() = {
        bind(classOf[Indexer]).asEagerSingleton()   // I suspect this needs to change
    }
}

// Indexer.scala
@Singleton
class Indexer @Inject() (appLifecycle: ApplicationLifecycle, system: ActorSystem) (implicit ec: ExecutionContext) { ... } 

在播放文档there is an example of injecting an actor system中,但只有在注入扩展Controller的类时才会起作用:

@Singleton
class MyController @Inject()(system: ActorSystem)(implicit exec: ExecutionContext) extends Controller {

    // This works

}

1 个答案:

答案 0 :(得分:1)

发现问题:

在Module.scala中我使用的是com.google.inject

import com.google.inject.AbstractModule  // <-------
import services.Indexer

class Module extends AbstractModule {
    override def configure() = {
        bind(classOf[Indexer]).asEagerSingleton()
    }
}

但在我的Indexer.scala服务中,我使用的是javax.inject包。

我已将所有文件切换到现在:

import com.google.inject._