3

我需要在我的类型安全配置中包含一些属性文件,比如

include ${user.HOME}"/.app/db-test.conf"

然而解析器抱怨:

com.typesafe.config.ConfigException$Parse: dev/application.conf: 47: include keyword is not followed by a quoted string, but by: '${'user.HOME'}'
com.typesafe.config.ConfigException$Parse: dev/application.conf: 47: include keyword is not followed by a quoted string, but by: '${'user.HOME'}'
    at com.typesafe.config.impl.Parser$ParseContext.parseError(Parser.java:329)
    at com.typesafe.config.impl.Parser$ParseContext.parseError(Parser.java:325)
    at com.typesafe.config.impl.Parser$ParseContext.parseInclude(Parser.java:574)
    at com.typesafe.config.impl.Parser$ParseContext.parseObject(Parser.java:624)
    at com.typesafe.config.impl.Parser$ParseContext.parseValue(Parser.java:408)
    at com.typesafe.config.impl.Parser$ParseContext.parseObject(Parser.java:657)
    at com.typesafe.config.impl.Parser$ParseContext.parse(Parser.java:832)
    at com.typesafe.config.impl.Parser.parse(Parser.java:34)

如何在包含语句中使用系统属性/环境变量?

4

2 回答 2

1

您可以在加载配置的代码中手动执行此操作吗?

Config baseConfig = ConfigFactory.load();
// Probably want error checking here.
Config testConfig = ConfigFactory.parseFile(
    new File(System.getenv("HOME") + "/.app/db-test.conf"));
// You may need to change your resolution order, depending on what you're doing in your 
// default config.
testConfig.resolve();
Config finalConfig = baseConfig.withFallback(testConfig);
于 2014-01-27T17:36:02.103 回答
0

目前无法开箱即用(请参阅https://github.com/typesafehub/config/issues/122)。

ConfigIncluder但是,如果您正在使用可以修改的代码配置应用程序,您可能会编写自定义代码。见http://typesafehub.github.io/config/latest/api/com/typesafe/config/ConfigIncluder.html

您还可以使用标准 Java 机制将 URL 方案添加到 Java(请参阅使用 URI 类创建自定义 URI 方案),然后使用include url("myscheme:whatever")

于 2014-01-11T01:01:53.733 回答