batman
batman

Reputation: 269

How the Env config substitution works

I am not able to substitute the env variables in application.properties

maven dependency:

<dependency>
    <groupId>com.typesafe</groupId>
    <artifactId>config</artifactId>
    <version>1.3.4</version>
</dependency>

application.properties present in resources test=${FORCED_VARIABLE}

Code:

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

output:

test_value
${FORCED_VARIABLE}

I am new to scala world, please suggest me what is that i am doing wrong

Upvotes: 0

Views: 151

Answers (1)

Chaitanya
Chaitanya

Reputation: 3638

You need to provide your file name in configFactory.load() method

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

If you see the documentation of ConfigFactory for load() method

/**
 * 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);
}

you will notice that load() method loads default configuration. Hope this helps !!!

Upvotes: 1

Related Questions