Dependency injection with Akka

巧了我就是萌 提交于 2019-12-12 08:36:23

问题


I use Guice in my application quite a lot. Recently i start to learn akka actors and felt like refactoring my application with it.

However upfront i am already wondering how all my guice will work with actors. I went on searching on google and it is kinda a bit messy.

The most up to date docs that i have found on the subject are theses:

http://letitcrash.com/post/55958814293/akka-dependency-injection

http://eng.42go.com/tag/guice/

which do not advocate same thing.

I must confess i still need to read a lot, i am at the beginning of learning akka. I Did few examples and red few things, but i don't want to go to deep into something to realize later that i will have many problems.

So my question is as of today, what is the consensus on how to use Akka Actors with dependency injection.

What kind of injection is possible ? Can we wire actors with object/other actors/....

Can anyway please outline in a concise way something that can help me to understand what is possible and what is the best practices ?


回答1:


The latest activator has a tutorial for Akka with Guice.




回答2:


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."



来源:https://stackoverflow.com/questions/20875368/dependency-injection-with-akka

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