简体   繁体   English

如何重用Typesafe配置文件中的属性?

[英]How reuse properties in typesafe config files?

Consider following files: 考虑以下文件:

application.conf application.conf

app {
    port = 5000
}

reference.conf reference.conf

akka {
    cluster {
        seed-nodes = ["akka.tcp://sysName@localhost:"${app.port}]
    }
}

So when I run ConfigFactory.load() it fails, because ${app.port} does not present in reference.conf. 因此,当我运行ConfigFactory.load()它将失败,因为${app.port}未出现在reference.conf中。

But load algorithm is clear - reference.conf is loaded and merged with application.conf . 但是加载算法很明确reference.conf已加载并与application.conf合并。 Is there a way to load application.conf and "include" reference.conf into it? 有没有一种方法可以将application.conf和“ include” reference.conf加载到其中?

IMPORTANT 重要

I tried add include "reference.conf" at the first line in application.conf it does not help. 我试过在application.conf的第一行添加include "reference.conf"并没有帮助。

You can use file("") syntax to include file. 您可以使用file("")语法包含文件。

include file("reference.conf")

see https://github.com/typesafehub/config/blob/master/HOCON.md#include-syntax 参见https://github.com/typesafehub/config/blob/master/HOCON.md#include-syntax

According to Typesafe Config documentation : 根据Typesafe Config 文档

The implication of this is that the reference.conf stack has to be self-contained; 这就意味着reference.conf堆栈必须是独立的。 you can't leave an undefined value ${foo.bar} to be provided by application.conf , or refer to ${foo.bar} in a way that you want to allow application.conf to override. 您不能保留由application.conf提供的未定义值${foo.bar} ,或者以您希望允许application.conf覆盖的方式引用${foo.bar} However, application.conf can refer to a ${foo.bar} in reference.conf . 但是, application.conf可以在reference.confreference.conf ${foo.bar}

The documentation also lists some possible workarounds: 该文档还列出了一些可能的解决方法:

  • putting an application.conf in a library jar, alongside the reference.conf , with values intended for later resolution. application.confreference.conf一起放在库jar中,其值供以后解决。
  • putting some logic in code instead of building up values in the config itself. 在代码中添加一些逻辑,而不是在配置本身中建立值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM