[JAVA] About Eclipse MicroProfile Config

Introduction

I would like to explain the Eclipse MicroProfile Config that was introduced in the previous What is Eclipse MicroProfile.

How to get started

Introduced in the previous How to get started with Eclipse Micro Profile, We will use MicroProfile Starter.

image.png

Create and download a project with this setting.

The file structure is as follows.

image.png

When you build, start the application and access localhost: 8080, the Top screen is prepared as shown below.

image.png

Unlike the last time, there is a sample link about Config. Both links are as simple as displaying the following settings on the screen.

image.png

How to set

Use of configuration file

The basic system is to look at the configuration file in the project. It can be set in Key-Value format.

microprofile-config.properties


injected.value=Injected value
value=lookup value

Use of startup arguments

Specify it as a startup argument in the following form.

java -Dinjected.value=CustomValue -jar target/configdemo.jar 

When started, the contents of the configuration file will be overwritten with the settings of the startup arguments as shown below.

image.png

Use of environment settings

You can also specify it with an environment variable.

export value=EnvCustomValue
java -jar target/configdemo.jar 
image.png

How to get the settings

Acquisition by injection

ConfigTestController.java


    //Use @Inject of CDI
    @Inject
    //Field injection is possible by acquiring the key name
    @ConfigProperty(name = "injected.value")
    private String injectedValue;

    @Path("/injected")
    @GET
    public String getInjectedConfigValue() {
        return "Config value as Injected by CDI " + injectedValue;
    }

Get with LookUp API

ConfigTestController.java


    @Path("/lookup")
    @GET
    public String getLookupConfigValue() {
        //Obtained with the following API
        Config config = ConfigProvider.getConfig();
        String value = config.getValue("value", String.class);
        return "Config value from ConfigProvider " + value;
    }

Summary

You can see that it was a fairly simple setting method and setting acquisition method. You can do the same with Spring, but you can easily use it with Java EE applications. You want to use this function when you use the same module, binary, and container image in the cloud or container.

Reference link

--Official document Configuration for MicroProfile

Recommended Posts

About Eclipse MicroProfile Config
About MicroProfile Fault Tolerance
Eclipse MicroProfile JAX-RS implementation
What's cool about Eclipse Collections What's not cool
About =
About Eclipse environment (Java, Liberty, JavaScript)
A little summary about typesafe config
Get in touch with Eclipse MicroProfile Health