I would like to explain the Eclipse MicroProfile Config that was introduced in the previous What is Eclipse MicroProfile.
Introduced in the previous How to get started with Eclipse Micro Profile, We will use MicroProfile Starter.
Create and download a project with this setting.
The file structure is as follows.
When you build, start the application and access localhost: 8080, the Top screen is prepared as shown below.
Unlike the last time, there is a sample link about Config. Both links are as simple as displaying the following settings on the screen.
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
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.
You can also specify it with an environment variable.
export value=EnvCustomValue
java -jar target/configdemo.jar
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;
}
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;
}
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.
--Official document Configuration for MicroProfile
Recommended Posts