如何将 appConfig 值获取到对象中 Scala/Play 2.5

How to get an appConfig value into an object Scala/Play 2.5

我已经在 2 天的大部分时间里努力实现这一目标,并且我不断遇到阻碍,任何建议将不胜感激。

假设我有一个对象

object myObject{
    val connectionString = (string from config file here)
}

既然必须注入配置,我将如何将我在 appConfig 中定义的字符串放到这里。

谢谢杰克

假设您想阅读某些 class 中的配置,比如 UserService

@Singleton
class UserService @Inject() (val configuration: Configuration) {
   val name = configuration.underlying.getString("hello")
}

请注意,在这种情况下,配置文件将是 application.conf

在application.conf

name="something"

不要使用 Scala 对象,使用 class 和 Singleton 注释和 Inject 注释将配置对象注入现有的 class.