我在Play for Scala中有以下类注入另一个类:
class MainEtl2 @Inject() (ic: injectedClass) {
def run (option: String) = {
ic.method1()
// ... some code
}
}
我需要在Akka Actor中调用方法run
。这是我的尝试,假设Guice会在调用injectedClass
时注入MainEtl2
:
class MainEtl extends Actor {
@Inject val me2 : MainEtl2
def receive = {
case option: String => {
val x = me2.run(option)
// ... more code
}
}
}
MainEtl
类无法使用followint错误进行编译:
class MainEtl needs to be abstract, since value me2 is not defined
如何使这项工作?
答案 0 :(得分:3)
我会基于Play documentation提出这样的解决方案。以这种方式定义你的演员:
class MainEtl @Inject() (me2: MainEtl2) extends Actor {
def receive = {
case option: String => {
val x = me2.run(option)
}
}
}
使用Akka支持定义play模块并绑定命名actor:
class AkkaBindings extends AbstractModule with AkkaGuiceSupport {
bindActor[MainEtl]("mainEtl")
}
通过添加
在application.conf中注册此模块play.modules.enabled += "some.package.AkkaBindings"
现在您可以通过名称引用注入您的actor:
class Scheduler @Inject()(@Named("mainEtl") mainEtl: ActorRef) {
//some code
val scheduler = QuartzSchedulerExtension(system)
scheduler.schedule("dailyproc", mainEtl, "abc", None)
}
答案 1 :(得分:1)
我会尝试注入MainEtl2
,类似this example注入CountingService
的方式:
class MainEtl @Inject() (me2: MainEtl2) extends Actor {
def receive = {
case option: String => {
val x = me2.run(option)
// ... more code
}
}
}
答案 2 :(得分:0)
虽然您正在指定@Inject注释,但仍需要一个初始值,guice会在注入依赖项时覆盖它,所以试试这个,
users