В Play 2.3 мы можем создать расширение, чтобы установить инжектор по умолчанию для инъекции зависимостей Akka. После того, как мы перешли на 2.4, нам больше не нужно создавать наш инжектор. Как можно повторно использовать Play's injector
для приведения зависимостей актеру Акка?Play 2.4 зависимая инъекция в аккордах Akka
У нас есть GuiceExtensionProvider
вроде этого:
/**
* An Akka Extension Provider
*/
object GuiceExtensionProvider extends ExtensionId[GuiceExtension] with ExtensionIdProvider {
override def lookup = GuiceExtensionProvider
/**
* Is used by Akka to instantiate the Extension identified by this ExtensionId, internal use only.
*/
override def createExtension(system: ExtendedActorSystem): GuiceExtension = new GuiceExtension(system)
}
/**
* The Extension implementation.
*/
class GuiceExtension(system: ExtendedActorSystem) extends Extension {
private var injector: Injector = _
/**
* Used to initialize the Guice Injector for the extension.
*/
def initialize(injector: Injector) = this.injector = injector
/**
* Create a Props for the specified actorType using the GuiceActorProducer class.
*
* @param actorType The type of the actor to create Props for
* @return a Props that will create the typed actor bean using Guice
*/
def props(actorType: Type): Props = Props(classOf[GuiceActorProducer], injector, actorType)
}
При запуске системы, мы будем называть их, чтобы инициализировать расширение:
class MyModule extends ScalaModule {
def configure() {
}
}
val injector = Guice.createInjector(new MyModule()) <--- `How can we use the default injector from Play?`
GuiceExtensionProvider(Akka.system).initialize(injector)
Это, как мы использовали для инициализации актера: Akka.system.actorOf(GuiceExtensionProvider(Akka.system).props(classOf[EmailActor]), "emailActor")