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

在Quarkus中使用SmallRyeConfig读取application.properties变量失败

问题:Quarkus中使用SmallRye Config读取配置失败

环境与实现代码

我参照SmallRye Config文档,尝试在Quarkus服务中从application.properties读取配置,由于未使用CDI,实现如下:

application.properties

redis.hosts=${REDIS_HOSTS}
redis.port=${REDIS_PORT}
redis.password=${REDIS_PASSWORD}

注:已正确设置上述环境变量。

RedisConfigInterface

import java.util.Set;
import io.smallrye.config.ConfigMapping;

@ConfigMapping(prefix = "redis")
interface RedisConfigInterface {
    Set<String> hosts();

    Integer port();

    String password();
}

ConfigCreation类

public class ConfigCreation {
            
    public ConfigCreation() {
    
        // 注册配置
        new SmallRyeConfigBuilder()
            .withMapping(RedisConfigInterface.class)
            .build();
        
        // 获取配置
        RedisConfigInterface config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getConfigMapping(RedisConfigInterface.class);
        
        Integer port = config.port();
        System.out.println(port);
    }
}

错误信息

服务构建正常,但启动时出现以下错误:

12:17:02 ERROR [io.qu.ru.Application] (Quarkus Main Thread)  Failed to start application (with profile dev): io.smallrye.config.ConfigValidationException: Configuration validation failed:
    java.util.NoSuchElementException: SRCFG00014: The config property redis.hosts is required but it could not be found in any config source
    java.util.NoSuchElementException: SRCFG00014: The config property redis.password is required but it could not be found in any config source
    java.util.NoSuchElementException: SRCFG00014: The config property redis.port is required but it could not be found in any config source

疑问

我是否遗漏了某些步骤?看起来所有配置都已正确设置。


内容的提问来源于stack exchange,提问作者Luiz Henrique

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 11:25:21