Reputation: 875
I have an Akka Actor in my Play app that accesses Play's configuration using a now deprecated method.
class MyActor (supervisor: ActorRef) extends Actor {
val loc = Play.current.configuration.getString("my.location").get
def receive = { case _ => }
}
if I do this:
import javax.inject._
class MyActor @Inject(configuration: play.api.Configuration) (supervisor: ActorRef) extends Actor {
My class won't compile and the compler returns: "classfile annotation arguments have to be supplied as named arguments"
I assume you can only DI the configuration within a controller class. So, is it possible to access the configuration from within an Akka Actore within a Play app? I could pass the configuration to the actor during construction or just have a separate config file for the actors, but both seem pretty hacky. Is there a preferred method using the Play api?
Thanks!
Upvotes: 1
Views: 444
Reputation: 595
The answer by mana above points out the most elegant way to use DI in combination with actors in Play, but within any Actor you can find the configuration like:
context.system.settings.config
Upvotes: 2
Reputation: 6547
This is working in my project:
Module.scala
:
class ExampleModule extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bindActor[ExampleActor]("example-actor-name")
}
}
Actor.scala
:
object ExampleActor {
def props = Props[ExampleActor]
}
@Singleton
class ExampleActor @Inject()(/*some DI*/) extends Actor {
...
}
And you can then even inject that very actor into other Classes (the @Named()
is optional if you have only one Actor
configured) via DI:
SomeOtherClass.scala
@Singleton
class SomeOtherClass @Inject()(@Named("example-actor-name") exampleActor: ActorRef) {
...
}
Upvotes: 1