我正在看看来自play-mailer的scaly代码示例:https://github.com/playframework/play-mailer
它基本上是这样的:
class MyComponent @Inject() (mailerClient: MailerClient) {
...
}
Run Code Online (Sandbox Code Playgroud)
很简单,它编译时没有顺从
然后我尝试"调用"它然而似乎没有办法满足编译器或获得mailerClient的工作实例.
object AnObject {
val mailer = new MyComponent
def sendEmail = mailer.doStuff
}
[info] Compiling 1 Scala source to ...
[error] /SomeOne/SomePath/SomeFile.scala:30: not enough arguments for constructor MyComponent: (mailerClient: play.api.libs.mailer.MailerClient) MyComponent.
[error] Unspecified value parameter mailerClient.
[error] val mailer = new MyComponent
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
Run Code Online (Sandbox Code Playgroud)
我可能因为这个而接近了:
这表明通过@Inject从构造函数中删除并将其放在字段上,以下语法可能会起作用.
@Inject var mailerClient: MailerClient = null
Run Code Online (Sandbox Code Playgroud)
但是,当我们尝试运行任何需要该引用的东西时,我们仍然会得到null.
我正在阅读@Inject上可以找到的所有内容 …
玩2.4不鼓励使用GlobalSettings.onStart和整个Global对象.
我正在使用Play-slick,它在GitHub中有很好的DI示例,但它缺少一个如何进行数据库初始化的示例.
如何在使用DI时以及当GlobalSettings.onStart不可用时实现数据库初始化?
数据库初始化的常见情况是:如果在DEV模式下,请添加这些行,如果PROD执行这些行.想要的例子.