Skip to main content

Variable Basics

Config files can contain variables that get resolved dynamically using the previously-described VarResolver API.

Example
#--------------------------
# My section

#--------------------------
[MySection]

# A system property

locale = $S{java.locale, en_US}

# An environment variable

path = $E{PATH, unknown}

# Another value in this config file

anotherLocale = $C{MySection/locale}

# Look for system property, or env var if that doesn't exist, or a default value if that doesn't exist.

nested = $S{mySystemProperty,$E{MY_ENV_VAR,$C{MySection/anotherLocale}}}

# A POJO with embedded variables

aBean = {foo:'$S{foo}',baz:$C{MySection/anInt}}
Config config = Config.create().build();

Locale locale = config.get("MySection/locale").as(Locale.class).orElse(null);
String path = config.get("MySection/path").asString().orElse(null);
int sameAsAnInt = config.get("MySection/sameAsAnInt").asInteger().orElse(null);
ABean bean = config.get("MySection/aBean").as(ABean.class).orElse(null);

By default, Configs use the VarResolver.DEFAULT variable resolver which provides support for the following variables and constructs:

SystemPropertiesVar - $S{key[,default]}EnvVariablesVar - $E{key[,default]}ConfigVar - $C{key[,default]}

The variable resolver is controlled via the following setting:

Config.BuildervarResolver(VarResolver)

Additionally, the following method can be used to retrieve a Config with a different variable resolver:

Configresolving(VarResolverSession)