Dependency injection with Akka

折月煮酒 提交于 2019-12-04 03:01:36

The latest activator has a tutorial for Akka with Guice.

I know you are working in Akka with Guice and Scala, but Typesafe provides a tutorial describing how things work in Akka with Spring and Java. This can provide a good starting point for understanding how dependency injection fits into the Actor lifecycle for your situation.

Meanwhile, here is some sample code from their documentation for using a factory method to inject constructor arguments:

class DependencyInjector(applicationContext: AnyRef, beanName: String) extends IndirectActorProducer {
  override def actorClass = classOf[Actor]
  override def produce = // obtain fresh Actor instance from DI framework ...
}
val actorRef = system.actorOf(Props(classOf[DependencyInjector], applicationContext, "hello"), "helloBean")

Here are some guidelines compiled by Typesafe on the matter.

Finally, note the following from the documentation:

"When using a dependency injection framework, actor beans MUST NOT have singleton scope."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!