Skip to content

Instantly share code, notes, and snippets.

@OlegIlyenko
Created March 2, 2014 22:55
Show Gist options
  • Select an option

  • Save OlegIlyenko/9315211 to your computer and use it in GitHub Desktop.

Select an option

Save OlegIlyenko/9315211 to your computer and use it in GitHub Desktop.
// First you need to create module with actor system and actor bindings in it:
// IMPORTANT: `Actor` bindings should always be providers (bound with `toProvider` method,
// IMPORTANT: which will create new instances each time it gets injected)
implicit val module = new Module {
bind [ActorSystem] to ActorSystem("MySystem")
bind [GreetPrinter] toProvider new GreetPrinter
binding toProvider new GenericPrinter
bind [PrintStream] to Console.out
}
// Now you can inject them by using `AkkaInjectable`
// It can inject an actor ref with `injectActorRef` method or,
// if you want to create actor yourself, it can inject actor Props
// with `injectActorProps`
import scaldi.akka.AkkaInjectable._
implicit val system = inject [ActorSystem]
val greetPrinter = injectActorRef [GreetPrinter] (name = "myPrinter")
// or
val greetPrinter = system.actorOf(injectActorProps[GreetPrinter], "myPrinter")
greetPrinter ! Greeting("Hello")
// You can also mix-in `AkkaInjectable` in the `Actor` itself in order to inject actor refs
class GreetPrinter(implicit inj: Injector) extends Actor with AkkaInjectable {
val printer = injectActorRef [GenericPrinter]
def receive = {
case Greeting(message) => printer ! Print(message)
}
}
class GenericPrinter(implicit inj: Injector) extends Actor with AkkaInjectable {
val stream = inject [PrintStream]
def receive = {
case Print(text) => stream.println(text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment