You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何生成Quarkus应用配置属性列表?能否用SmallRye Config或Quarkus API?

实现自定义应用配置列表(含描述、默认值)

完全可以通过SmallRye Config或Quarkus原生API实现类似Quarkus官方配置列表的功能,以下是具体实现方案:

一、Quarkus原生实现(推荐)

Quarkus内置了配置元数据生成机制,无需额外依赖就能快速生成并读取配置信息:

  1. 注解化配置类
    用Quarkus/SmallRye提供的注解定义配置,同时补充描述和默认值:
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithDocumentation;

@ConfigMapping(prefix = "myapp")
public interface MyAppConfig {
    @WithDocumentation("应用显示名称,用于UI标识与日志输出")
    @WithDefault("My Awesome App")
    String appName();

    @WithDocumentation("服务监听的HTTP端口")
    @WithDefault("8080")
    int serverPort();

    // 嵌套配置示例
    DatabaseConfig database();

    interface DatabaseConfig {
        @WithDocumentation("数据库连接URL")
        @WithDefault("jdbc:h2:mem:defaultdb")
        String url();

        @WithDocumentation("数据库访问用户名")
        @WithDefault("sa")
        String username();
    }
}
  1. 自动生成元数据
    Quarkus在构建阶段会自动扫描带@ConfigMapping/@ConfigProperty的类,生成META-INF/quarkus-extension.yaml和META-INF/smallrye/config-metadata.yaml文件,文件中包含所有配置的键、描述、默认值、类型等信息。

  2. 读取元数据并展示
    通过Quarkus的ConfigMetadataProvider API直接加载元数据,用于后续渲染:

import io.quarkus.runtime.configuration.ConfigMetadata;
import io.quarkus.runtime.configuration.ConfigMetadataProvider;

// 获取元数据提供者实例
ConfigMetadataProvider metadataProvider = ConfigMetadataProvider.instance();
// 拿到所有配置元数据集合
Collection<ConfigMetadata> allConfigs = metadataProvider.getAllConfigMetadata();

// 遍历提取信息(示例)
for (ConfigMetadata config : allConfigs) {
    String configKey = config.getKey();
    String description = config.getDescription();
    String defaultValue = config.getDefaultValue();
    // 可将这些数据渲染为HTML表格、Markdown列表等格式
}

二、SmallRye Config通用实现(支持非Quarkus环境)

如果你的应用不基于Quarkus,单独使用SmallRye Config也能实现:

  1. 添加依赖
    确保项目中引入SmallRye Config核心包及元数据生成器:
<!-- Maven依赖示例 -->
<dependency>
    <groupId>io.smallrye</groupId>
    <artifactId>smallrye-config</artifactId>
    <version>最新版本</version>
</dependency>
<dependency>
    <groupId>io.smallrye</groupId>
    <artifactId>smallrye-config-metadata-generator</artifactId>
    <version>最新版本</version>
    <scope>provided</scope>
</dependency>
  1. 定义配置类
    和Quarkus环境一样,使用@ConfigMapping、@WithDocumentation、@WithDefault注解定义配置(同上面的代码示例)。

  2. 生成并读取元数据
    编译时元数据生成器会自动生成META-INF/smallrye/config-metadata.yaml,然后通过SmallRye的API加载:

import io.smallrye.config.ConfigMetadataRepository;
import io.smallrye.config.ConfigMetadataRepositoryLoader;

// 加载元数据仓库
ConfigMetadataRepository metadataRepo = ConfigMetadataRepositoryLoader.load();

// 遍历所有配置条目
metadataRepo.getAllEntries().forEach(entry -> {
    String key = entry.getKey();
    String description = entry.getDescription();
    String defaultValue = entry.getDefaultValue();
    // 处理并展示这些配置信息
});

注意事项

  • 确保配置类被扫描到:Quarkus默认扫描所有包,非Quarkus环境需指定扫描路径或手动注册配置类。
  • 定制化展示:可以将读取到的元数据转换为JSON、Markdown或直接渲染成网页,完全复刻Quarkus官方配置页面的样式。

内容的提问来源于stack exchange,提问作者Julio Faerman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 22:01:02