Reading properties from a configuration folder in WildFly

There are several approaches for reading properties from a directory in WildFly. We will show in this tutorial two alternative solutions:

Reading properties from a file using MicroProfile Config API

The recommended solution for Microservices deployments is to use the Eclipse MicroProfile Config which is a solution to externalise configuration from microservices. The config properties are provided by ConfigSources. ConfigSources are ordered according to their ordinal, with a higher value taking priority over a lower one.

An in-depth guide on MicroProfile Config API is available here: Configuring Microservices with MicroProfile Configuration

Here we will mention that WildFly ships with a ConfigSource which points to the META-INF/microprofile-config.properties file. All properties contained in that file, will be available to the application.

As an example, if the file microprofile-config.properties contains the following entries:

num.size=5
num.max=100

Then your application can use the num.size and num.max properties tby injecting the @ConfigProperty with their attribute:

@Inject
@ConfigProperty(name = "num.size")
int numSize; // will be 5

@Inject
@ConfigProperty(name = "num.max")
int numMax; // will be 100

Reading properties from a configuration directory using plain Java API

The standard approach for reading properties in any Java application is to use the java.util.Properties class which includes the load method to load properties from an InputStream. Here is an example which shows how to read Properties from a file named “file.properties” which is located in the configuration folder of the application server:

Properties prop = new Properties();

String fileName = System.getProperty("jboss.server.config.dir") + "/file.properties";
   try(FileInputStream fis = new FileInputStream(fileName)) {
      prop.load(fis);
}

// print all properties
prop.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v));

// get value by key
prop.getProperty("user");
prop.getProperty("password");

That’s it. In this tutorial we have covered two alternative solutions to read property files from a directory using MicroProfile config API and a pure Java solution.


Recommended Articles

7 Ways to Set and Load System Properties in WildFly / JBoss EAP

7 ways to set and load System Properties in WildFly and JBoss EAP: JVM args, standalone.xml, CLI, the --properties startup flag, modules, MicroProfile Config, and environment variables for Kubernetes/OpenShift.

How to Start WildFly in Read-Only Mode for Enhanced Security and Control

Learn how to start WildFly in read-only mode for enhanced security. Includes steps and commands.

WildFly Application Server Folder Structure Tutorial - Learn WildFly Structure

Discover the basic folder structure of WildFly application server and learn how to navigate its configuration, deployment, and data folders.

WildFly Embedded Mode for Command Line Interface - Simplified Configuration Management

Easily manage WildFly configurations offline with embedded mode. Learn how to use embed-server command and –server-config option for Cloud applications. #WildFlyCLI #JavaDev #CloudNative