spring - Getting context using AnnotationConfigApplicationContext in Akka Actor -
how annotation context . looking @ code (copied here) :
case object tick case object @named class countingservice { def increment(count: int) = count + 1 } @named @scope("prototype") class counter @inject() (countingservice: countingservice) extends actor { var count = 0 def receive = { case tick => count = countingservice.increment(count) case => sender ! count } } @configuration class appconfiguration { @bean def actorsystem = actorsystem("akkaspring") } object akkaspring extends app { val ctx = new annotationconfigapplicationcontext ctx.scan("org.typesafe") ctx.refresh() val system = ctx.getbean(classof[actorsystem]) val counter = system.actorof(props().withcreator(ctx.getbean(classof[counter]))) counter ! tick counter ! tick counter ! tick implicit val timeout = timeout(5 seconds) // wait result , print it, shut down services (counter ? get) andthen { case count ⇒ println("count " + count) } oncomplete { _ => system.shutdown() } }
but if want use the counter in actor e.g :
class counteruser extends actor { val countingservice = system.actorof(props().withcreator(ctx.getbean(classof[counter]))) def receive = { case tick => countingservice ! tick } }
how context in other actor ?
how can inject other annotationconfigapplicationcontext
testing ?
assuming counteruser
spring bean, can this:
class counteruser @autowired() (ctx: applicationcontext) extends actor { val countingservice = system.actorof(props().withcreator(ctx.getbean(classof[counter]))) def receive = { case tick => countingservice ! tick } }
here way same using akka extensions: https://github.com/bijukunjummen/akka-scala-spring/tree/upgrade-spring
Comments
Post a Comment