【问题标题】:include configs from ${HOME}/.app/someconfig.conf包括来自 ${HOME}/.app/someconfig.conf 的配置
【发布时间】:2013-10-05 03:17:07
【问题描述】:

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

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)

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

【问题讨论】:

    标签: config typesafe-stack typesafe


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

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

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

      您还可以使用标准 Java 机制将 URL 方案添加到 Java(请参阅Creating custom URI scheme using URI class) 然后使用include url("myscheme:whatever")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 2016-04-13
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多