4.2. MicroProfile Config の開発
4.2.1. MicroProfile Config の Maven プロジェクトの作成 リンクのコピーリンクがクリップボードにコピーされました!
必要な依存関係で Maven プロジェクトを作成し、MicroProfile Config アプリケーションを作成するためのディレクトリー構造を作成します。
前提条件
- Maven がインストールされている。
手順
Maven プロジェクトを設定します。
$ mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=microprofile-config \ -DinteractiveMode=false \ -DarchetypeGroupId=org.apache.maven.archetypes \ -DarchetypeArtifactId=maven-archetype-webapp cd microprofile-configこれにより、プロジェクトのディレクトリー構造と
pom.xml設定ファイルが作成されます。POM ファイルが
jboss-eap-xp-microprofileBOM の MicroProfile Config アーティファクトおよび MicroProfile REST Client アーティファクトのバージョンを自動的に管理できるようにするには、POM ファイルの<dependencyManagement>セクションに BOM をインポートします。<dependencyManagement> <dependencies> <!-- importing the microprofile BOM adds MicroProfile specs --> <dependency> <groupId>org.jboss.bom</groupId> <artifactId>jboss-eap-xp-microprofile</artifactId> <version>4.0.0.GA</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>BOM によって管理される MicroProfile Config アーティファクトおよび MicroProfile REST Client アーティファクトおよびその他依存関係をプロジェクト POM ファイルの
<dependency>セクションに追加します。以下の例は、MicroProfile Config および MicroProfile REST Client 依存関係をファイルに追加する方法を示しています。<!-- Add the MicroProfile REST Client API. Set provided for the <scope> tag, as the API is included in the server. --> <dependency> <groupId>org.eclipse.microprofile.rest.client</groupId> <artifactId>microprofile-rest-client-api</artifactId> <scope>provided</scope> </dependency> <!-- Add the MicroProfile Config API. Set provided for the <scope> tag, as the API is included in the server. --> <dependency> <groupId>org.eclipse.microprofile.config</groupId> <artifactId>microprofile-config-api</artifactId> <scope>provided</scope> </dependency> <!-- Add the {JAX-RS} API. Set provided for the <scope> tag, as the API is included in the server. --> <dependency> <groupId>org.jboss.spec.javax.ws.rs</groupId> <artifactId>jboss-jaxrs-api_2.1_spec</artifactId> <scope>provided</scope> </dependency> <!-- Add the CDI API. Set provided for the <scope> tag, as the API is included in the server. --> <dependency> <groupId>jakarta.enterprise</groupId> <artifactId>jakarta.enterprise.cdi-api</artifactId> <scope>provided</scope> </dependency>
4.2.2. アプリケーションでの MicroProfile Config プロパティーの使用 リンクのコピーリンクがクリップボードにコピーされました!
設定された ConfigSource を使用するアプリケーションを作成します。
前提条件
- JBoss EAP では MicroProfile Config が有効になります。
- 最新の POM がインストールされている。
- Maven プロジェクトは、MicroProfile Config アプリケーションを作成するために設定されます。
手順
クラスファイルを保存するディレクトリーを作成します。
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/config/APPLICATION_ROOTは、アプリケーションのpom.xml設定ファイルが含まれるディレクトリーです。新しいディレクトリーに移動します。
$ cd APPLICATION_ROOT/src/main/java/com/example/microprofile/config/このディレクトリーに、この手順で説明しているすべてのクラスファイルを作成します。
以下の内容でクラスファイル
HelloApplication.javaを作成します。package com.example.microprofile.config; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/") public class HelloApplication extends Application { }このクラスは、アプリケーションを Jakarta RESTful Web Services アプリケーションとして定義します。
以下の内容を含むクラスファイル
HelloService.javaを作成します。package com.example.microprofile.config; public class HelloService { String createHelloMessage(String name){ return "Hello " + name; } }以下の内容を含むクラスファイル
HelloWorld.javaを作成します。package com.example.microprofile.config; import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import org.eclipse.microprofile.config.inject.ConfigProperty; @Path("/config") public class HelloWorld { @Inject @ConfigProperty(name="name", defaultValue="jim")1 String name; @Inject HelloService helloService; @GET @Path("/json") @Produces({ "application/json" }) public String getHelloWorldJSON() { String message = helloService.createHelloMessage(name); return "{\"result\":\"" + message + "\"}"; } }- 1
- MicroProfile Config プロパティーは、
@ConfigProperty(name="name", defaultValue="jim")アノテーションでクラスにインジェクトされます。ConfigSourceが設定されていない場合、この値jimが返されます。
src/main/webapp/WEB-INF/ディレクトリーにbeans.xmlという名前の空のファイルを作成します。$ touch APPLICATION_ROOT/src/main/webapp/WEB-INF/beans.xmlAPPLICATION_ROOTは、アプリケーションのpom.xml設定ファイルが含まれるディレクトリーです。アプリケーションの root ディレクトリーに移動します。
$ cd APPLICATION_ROOTAPPLICATION_ROOTは、アプリケーションのpom.xml設定ファイルが含まれるディレクトリーです。プロジェクトをビルドします。
$ mvn clean install wildfly:deploy出力をテストします。
$ curl http://localhost:8080/microprofile-config/config/json以下が想定される出力です。
{"result":"Hello jim"}