Inject Kubernetes ConfigMap Values With Java EE and WildFly

DZone 's Guide to

Inject Kubernetes ConfigMap Values With Java EE and WildFly

If you're working with Kubernetes, you can snag your ConfigMap values and put them in your Java EE app. There's configuration work up front, but it's worth the effort.

· Java Zone ·
Free Resource

The Kubernetes ConfigMap concept is used to configure applications from an orchestration environment. The configured values can be made accessible within the Java EE container.

Kubernetes can inject configured values to the running POD, e.g. as properties files. By defining custom modules in WildFly, these files — which are not shipped with the deployment artifact — can be made accessible from the classpath.

First, define a Kubernetes ConfigMap via a YAML file:

kind: ConfigMap
apiVersion: v1
    name: hello-config
    application.properties: |

Alternatively, you can also define ConfigMaps directly from properties files: kubectl create configmap hello-config --from-file=application.properties

The properties entries of this ConfigMap are injected into the running POD in different ways, for instance via mounted volume, which results in a properties file being created, containing all the configured values.

Consider the following container definition in a deployment resource using WildFly as the application server and referring to the ConfigMap.

kind: Deployment
apiVersion: apps/v1beta1
    name: cloud-native-jee
    replicas: 1
                app: cloud-native-jee
            - name: cloud-native-jee
                image: cloud-native-jee:123
                - name: config-volume
                    mountPath: /opt/wildfly/modules/com/sebastian-daschner/configuration/main/properties
            - name: config-volume
                    name: hello-config
            restartPolicy: Always

The base image containing the WildFly application server has to configure an additional module. This is done by placing a module.xml file into a module directory <wildfly_home>/modules/com/sebastian-daschner/configuration/main:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="com.sebastian-daschner.configuration">
        <resource-root path="properties"/>

Also, declare the module in the standalone.xml configuration:

<subsystem xmlns="urn:jboss:domain:ee:4.0">
        <module name="com.sebastian-daschner.configuration" slot="main" />

This will access the new module and make all files residing under …/configuration/main/properties accessible from the classpath.

Now the running container can access the application.properties file. To make the development process more convenient, we define a custom CDI producer for the properties:

public class Configurator {

    private final Properties properties = new Properties();

    private void initProperties() {
        try (final InputStream inputStream = Configurator.class.getResourceAsStream("/application.properties")) {
        } catch (IOException e) {
            throw new RuntimeException("Could not init configuration", e);

    public String exposeConfig(InjectionPoint injectionPoint) {
        final Config config = injectionPoint.getAnnotated().getAnnotation(Config.class);
        if (config != null)
            return properties.getProperty(config.value());
        return null;


The @Config annotation is defined in our application to select the specific keys:

public @interface Config {

    String value();


Now we can @Inject values from anywhere in our application:

public class HelloResource {

    String greeting;

    String name;

    public String hello() {
        return greeting + ", " + name + "!";


Happy Kubernetes configuring!

java, java ee, kubernetes, tutorial, wildfly

Published at DZone with permission of Sebastian Daschner , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}