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

Spring Boot中@Configuration类无法解析@Value注入的YML配置

@Value注入YAML数组失败问题排查(Spring Boot 3.1.4)

预期可行的配置与代码

按照常规Spring配置方案,以下写法应该能实现@Value注入YAML中的数组:

application-dev.yml配置

# application-dev.yml
gateway:
  publicEndpoints:
    - /api/v1/example
    - /api/v1/another-example

注:最初配置中每个数组项末尾带有逗号,已尝试移除逗号但问题依旧。

构造器注入实现

@Configuration
@EnableWebFluxSecurity // 该注解是否会影响注入?
public class SecurityConfig {
    private final String[] publicEndpoints;

    public SecurityConfig(@Value("${gateway.publicEndpoints}") String[] publicEndpoints) {
        this.publicEndpoints = publicEndpoints; // 此处断点从未触发
    }

    // ... 其他业务代码
}

字段注入实现

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
    @Value("${gateway.publicEndpoints}")
    private String[] publicEndpoints;

    // ... 其他业务代码
}

实际运行异常

无论采用构造器注入还是字段注入,项目启动时都会抛出以下异常:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'gateway.publicEndpoints' in value "${gateway.publicEndpoints}"

已完成的排查动作

  • 确认激活配置正确:启动日志明确显示The following 1 profile is active: "dev"
  • 检查YAML文件缩进,确认无格式错误
  • 移除数组项末尾的逗号,问题未得到解决

已知可行方案与疑问

使用@ConfigurationProperties注解可以成功注入该数组,但仍有疑问待解答:

  • 曾猜测YAML格式本身导致@Value不支持,但官方文档并未证实这一观点
  • 在配置项存在、环境激活正确的前提下,为何@Value无法解析gateway.publicEndpoints这个占位符?

内容的提问来源于stack exchange,提问作者Sergey Zolotarev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 20:45:55