If you’re in the Enterprise Java space, you’re probably aware that you can use MicroProfile Config to configure your applications. This also works with plain Java projects, without an enterprise runtime, by using a MicroProfile implementation such as Smallrye. In this video, I’m showing how to do this, and in which circumstances it might be helpful:
The example uses a programmatic lookup via
String value = ConfigProvider.getConfig().getValue("config.value", String.class); System.out.println("value = " + value);
In the video, I’m making use of another
ConfigSource that reads the entries from a
.env file, which overrides entries in
# META-INF/microprofile-config.properties config.value=123 ...
# .env, in current working directory CONFIG_VALUE=234 ...
For this example to work, we need to activate Smallrye’s
DotEnvConfigSourceProvider via Java Service Loader, by adding a file
META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider that contains the fully-qualified class name:
You can check out the
ConfigExample code on GitHub.
In order to see a fully-fledged example which uses this approach, have a look at my post on Running complex project setups with Testcontainers.
Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Configuring plain Java projects using MicroProfile Config
Opinions expressed by Java Code Geeks contributors are their own.