How to use system properties to substitute placeholders in Typesafe Config file?

只谈情不闲聊 提交于 2019-12-20 17:39:15

问题


I need to refer to java.io.tmpdir in my application.conf file

I printed content of my config with

val c = ConfigFactory.load()
System.err.println(c.root().render())

and it renders it like

# dev/application.conf: 1
"myapp" : {
    # dev/application.conf: 47
    "db" : {
        # dev/application.conf: 49
        "driver" : "org.h2.Driver",
        # dev/application.conf: 48
        "url" : "jdbc:h2:file:${java.io.tmpdir}/db;DB_CLOSE_DELAY=-1"
    }
 ...
 }
# system properties
"java" : {
    # system properties
    "io" : {
        # system properties
        "tmpdir" : "/tmp"
    },
....

So I guess that forward-reference does not work. Is there any way to get my options loaded after system properties, so config parser will correctly substitute values?


回答1:


Forward references work fine; I believe the issue is just that you have the ${} syntax inside of quotes, so it doesn't have special meaning. Try it like this:

url = "jdbc:h2:file:"${java.io.tmpdir}"/db;DB_CLOSE_DELAY=-1"

(note that the ${} stuff is not quoted)

In the HOCON format, anything that's valid JSON will be interpreted as it would be in JSON, so quoted strings for example don't have special syntax inside them, other than the escape sequences JSON supports.



来源:https://stackoverflow.com/questions/18520235/how-to-use-system-properties-to-substitute-placeholders-in-typesafe-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!