iCo*_*unk 4 dependency-injection scala guice akka playframework
我能够毫无问题地将服务注入Application类。但是我无法以某种方式注入演员本身。
我的演员:
class PollerCrow @Inject()(
@Named("pollService") pollService: PollService[List[ChannelSftp#LsEntry]]
, @Named("redisStatusService") redisStatusService: StatusService
, @Named("dynamoDBStatusService") dynamoDbStatusService: StatusService
) extends BaseCrow {
... impl and stuff ...
}
Run Code Online (Sandbox Code Playgroud)
我的演员的同伴对象:
object PollerCrow extends NamedActor {
override def name: String = this.getClass.getSimpleName
val filesToProcess = ConfigFactory.load().getString("poller.crow.files.to.process")
def props = Props[PollerCrow]
}
Run Code Online (Sandbox Code Playgroud)
运行它时,我得到以下信息:
IllegalArgumentException: no matching constructor found on class watcher.crows.PollerCrow for arguments []
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
编辑:
我已经绑定了演员:
class ActorModule extends AbstractModule with AkkaGuiceSupport {
override def configure() {
bindPollerActors()
}
private def PollActors() = {
bindActor[PollerCrow](PollerCrow.name)
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2:
该课程的其他详细信息:
abstract class BaseCrow extends Crow with Actor with ActorLogging
class PollerCrow @Inject()(
@Named(ServiceNames.PollService) pollService: PollService[List[ChannelSftp#LsEntry]]
, @Named(ServiceNames.RedisStatusService) redisStatusService: StatusService
, @Named(ServiceNames.DynamoDbStatusService) dynamoDbStatusService: StatusService
) extends BaseCrow {
override def receive: Receive = {
...
}
}
object PollerCrow extends NamedActor {
override def name: String = this.getClass.getSimpleName
def props = Props[PollerCrow]
}
trait NamedActor {
def name: String
final def uniqueGeneratedName: String = name + Random.nextInt(10000)
}
Run Code Online (Sandbox Code Playgroud)
您可能会Guice意识到自己的演员。这是干净的方法:
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
class ActorModule extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bindActor[YourActor]("your-actor")
}
}
@Singleton
class YourActor @Inject()(yourService: IYourService) extends Actor {
override def receive: Receive = {
case msg => unhandled(msg)
}
}
Run Code Online (Sandbox Code Playgroud)
和application.conf:
play.modules {
enabled += "ActorModule"
}
Run Code Online (Sandbox Code Playgroud)
对于那些不想麻烦的人,只需直接致电注射器,别忘了导入Application示波器:
Play.application.injector.instanceOf[YourService]
Play.application.injector.instanceOf(BindingKey(classOf[YourService]).qualifiedWith("your-name"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |