Env配置替换的工作方式

时间:2019-04-23 05:44:26

标签: scala typesafe-config

我无法替换application.properties中的env变量

maven依赖项:

<dependency>
    <groupId>com.typesafe</groupId>
    <artifactId>config</artifactId>
    <version>1.3.4</version>
</dependency>
资源中存在

application.propertiestest=${FORCED_VARIABLE}

代码:

var config = ConfigFactory.load()
println(System.getenv("FORCED_VARIABLE"))
println(config.resolve().getString("test"))

输出:

test_value
${FORCED_VARIABLE}

我是scala世界的新手,请告诉我我做错了什么

1 个答案:

答案 0 :(得分:1)

您需要在configFactory.load()方法中提供文件名

lazy val configFile = "application.properties"
lazy val config = ConfigFactory.load(configFile)
lazy val variableFromFile: String = config.getString("test")

如果您看到ConfigFactory的有关load()方法的文档

/**
 * Loads a default configuration, equivalent to {@link #load(Config)
 * load(defaultApplication())} in most cases. This configuration should be used by
 * libraries and frameworks unless an application provides a different one.
 * <p>
 * This method may return a cached singleton so will not see changes to
 * system properties or config files. (Use {@link #invalidateCaches()} to
 * force it to reload.)
 *
 * @return configuration for an application
 */
public static Config load() {
    ClassLoader loader = checkedContextClassLoader("load");
    return load(loader);
}

您会注意到load()方法加载默认配置。 希望这可以帮助!!!