【发布时间】:2017-03-21 19:59:26
【问题描述】:
当我从字符串解析配置时,我可以解决替换问题,但从映射或文件解析时却不行。
import java.io.File
import com.typesafe.config.{Config, ConfigFactory}
import scala.collection.JavaConversions.mapAsJavaMap
val s: String = "a = test, b = another ${a}"
val m: Map[String, String] = Map("a" -> "test", "b" -> "another ${a}")
val f: File = new File("test.properties") // contains "a = test\nb = another ${a}"
val cs: Config = ConfigFactory.parseString(s).resolve
val cm: Config = ConfigFactory.parseMap(mapAsJavaMap(m)).resolve
val cf: Config = ConfigFactory.parseFile(f).resolve
println("b from string = " + cs.getString("b"))
println("b from map = " + cm.getString("b"))
println("b from file = " + cf.getString("b"))
> b from string = another test
> b from map = another ${a}
> b from file = another ${a}
当我没有立即解决时,可以看出变量占位符并没有真正以同样的方式处理:
val cs: Config = ConfigFactory.parseString(s)
val cm: Config = ConfigFactory.parseMap(mapAsJavaMap(m))
val cf: Config = ConfigFactory.parseFile(f)
> cs: com.typesafe.config.Config = Config(SimpleConfigObject({"a":"test","b":"another "${a}}))
> cm: com.typesafe.config.Config = Config(SimpleConfigObject({"a":"test","b":"another ${a}"}))
> cf: com.typesafe.config.Config = Config(SimpleConfigObject({"a":"test","b":"another ${a}"}))
我可能只是将地图/文件转换为字符串,但有没有办法让库处理它?
【问题讨论】:
-
在图书馆的Github page 上有一个问题,或多或少问同样的问题,解释类似于下面 Federico 的回答。
标签: scala config typesafe-config