Home » Java » Enterprise Java » Configuring plain Java projects using MicroProfile Config

About Sebastian Daschner

Sebastian Daschner
Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.

Configuring plain Java projects using MicroProfile Config

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 ConfigProvider:

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 microprofile-config.properties:

# 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:

io.smallrye.config.DotEnvConfigSourceProvider

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments