4.2. MicroProfile 配置开发
4.2.1. 为 MicroProfile 配置创建 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 文件自动管理 MicroProfile 配置构件的版本和
jboss-eap-xp-microprofileBOM 中的 MicroProfile REST 客户端构件,请将 BOM 导入到项目 POM 文件的 <dependencyManagement> 部分。<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>将 MicroProfile Config 构件和 MicroProfile REST 客户端构件和其他依赖项(由 BOM 管理)添加到项目 POM 文件的
<dependency> 部分。以下示例演示了将 MicroProfile 配置和 MicroProfile REST 客户端依赖项添加到该文件中:<!-- 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 配置。
- 已安装最新的 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.xml其中
APPLICATION_ROOT是包含应用的pom.xml配置文件的目录。进入应用程序的根目录:
$ cd APPLICATION_ROOT其中
APPLICATION_ROOT是包含应用的pom.xml配置文件的目录。构建项目:
$ mvn clean install wildfly:deploy测试输出:
$ curl http://localhost:8080/microprofile-config/config/json以下是预期的输出:
{"result":"Hello jim"}