我有一个代码,可以从“ conf.property”文件中加载属性。是否有更好的选择来加载所有属性并将其存储在例如map中?当前方法强制为每个其他属性添加一行代码。
import java.io.File
object SomeObject {
// path to the property file
val path = "/src/main/resources/conf.properties"
// load configuration from file
val conf = ConfigFactory.parseFile(new File(path))
// get properties
val prDataPath = conf.getString("dataPath")
val prContainsHeader = conf.getBoolean("containsHeader").toString
val prMaxRows: Option[Int] = Try(conf.getInt("maxRows")).toOption
// more config variables here...
}
Run Code Online (Sandbox Code Playgroud)
您可以使用PureConfig
你的 application.conf
prop1 = "hello"
prop2 = "okidoki"
Run Code Online (Sandbox Code Playgroud)
如果像您的示例中那样加载,这应该可以工作(我尚未测试):
import pureconfig.generic.auto._
loadConfig[Map[String, String](conf)
Run Code Online (Sandbox Code Playgroud)
您还可以创建一个case class代表您的配置的。
你的例子:
case class MyConfig(dataPath: String, containsHeader: Boolean, maxRows: Option[Int])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |