WebSocket.acceptWithActor
instantiates a new Akka actor without making use of Guice.
With Play 2.4, using the injector for my actor was still possible by importing play.api.Play.current
.
Snippet from ReactiveMongo documentation:
import scala.concurrent.Future
import play.api.Play.current // should be deprecated in favor of DI
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json.collection.JSONCollection
object Foo {
lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
def collection(name: String): Future[JSONCollection] =
reactiveMongoApi.database.map(_.collection[JSONCollection](name))
}
But in Play 2.5, play.api.Play.current
is deprecated. How can I still inject ReactiveMongoApi
in my actor? What is the recommended way of using an instance of ReactiveMongoApi
in my actor?
Here is my code which works with Play 2.4 because my custom actor class ClientActor
has access to ReactiveMongoApi
through current.injector.instanceOf[ReactiveMongoApi]
:
@Singleton
class Application @Inject() (system: ActorSystem) extends Controller {
val midiDiscoveryActor = system.actorOf(MidiDiscoveryActor.props, "midi-discovery-actor")
val midiActor = system.actorOf(MidiActor.props(midiDiscoveryActor), "midi-actor")
def index(page: String) = Action {
Ok(views.html.index(page))
}
def bidirectional = WebSocket.acceptWithActor[JsValue, JsValue] { request => out =>
ClientActor.props(out, midiActor, midiDiscoveryActor)
}
}
I don't think this is possible. Quoting James Roper:
Play 2.5 has built in support for DI.
MidiActor signature needs to be modified as said below.
Create new Module and enable in application.conf
Change your controller as below