【问题标题】:Read application.conf configuration in Play for Scala阅读 Play for Scala 中的 application.conf 配置
【发布时间】:2016-11-10 12:37:02
【问题描述】:

我在application.conf 中配置了以下数据源,用于运行 Slick 语句。

我需要通过使用相同用户和密码的 OLAP 引擎访问同一个数据库。

既然已经配置好了,我想从那里得到这两个,是否可以从 Scala 中读取application.conf?我知道我可以读取物理文件,但是是否有一个库可以获取解析的数据?

## JDBC Datasource
# ~~~~~
#

dbConfig = {
  url = "jdbc:mysql://localhost:3306/db"
  driver = com.mysql.jdbc.Driver
  connectionPool = disabled
  keepAliveConnection = true
  user=root
  password=xxxx
}

【问题讨论】:

  • 你用的是哪个版本的play?
  • 通过@m-z typesafe 配置链接是你需要的。但是如果你使用的是 Play 2.5,你可以在需要的地方注入配置。

标签: scala playframework playframework-2.0


【解决方案1】:

使用 play,您只需像这样注入配置:

import javax.inject.Inject
import play.api.Configuration

class Something @Inject()(configuration: Configuration) {
  val url: Option[String] = configuration.getString("dbConfig.url")
  val keepAliveConnection: Option[Boolean] = configuration.getBoolean("dbConfig.keepAliveConnection")

  ...
}

另请参阅Configuration API,了解如何获取各种类型和格式的属性。

【讨论】:

  • 工作得很好,我只想在Option 中添加返回值。将我自己的值存储在 application.conf 中并将其用作属性文件是一种好习惯吗?
  • 添加了选项类型以进行说明。是的,这是一个很好的做法。 Production Configuration 可能对你也有用。
  • 也许可以添加到您的答案中:Imports: import javax.inject.Inject import play.api.Configuration
猜你喜欢
  • 1970-01-01
  • 2017-09-10
  • 2017-05-18
  • 2019-02-11
  • 1970-01-01
  • 2018-02-12
  • 2021-03-22
  • 2013-01-14
  • 1970-01-01
相关资源
最近更新 更多