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

Spring Boot中用@Value读取YAML多变量配置遇占位符错误

问题分析与解决方案

你遇到的核心问题是:@Value注解无法直接将整个YAML配置节点绑定到自定义对象。@Value仅适用于读取单个简单属性(如字符串、数字、基础集合),复杂的嵌套配置/自定义对象需要使用Spring Boot提供的@ConfigurationProperties来处理。此外你的YAML配置中based-city的写法不符合Map类型的语法规范,也会导致解析失败。

步骤1:修正YAML配置格式

首先调整based-city的写法,YAML中Map类型需要用键值对缩进或大括号包裹,而非列表语法:

apple:
  color: [red, green, white]
  based-city:
    jammu: red
    maharastra: green

banana:
  color: [red, green, yellow]
  based-city:
    jammu: red
    karnataka: green
    nagpur: yellow

步骤2:完善自定义类并使用配置绑定

方式1:创建子类分别绑定不同配置节点

先定义父类Fruit:

import java.util.List;
import java.util.Map;

public class Fruit {
    private List<String> color;
    private Map<String, String> basedCity;

    // 必须提供Getter/Setter,Spring需要通过它们注入属性
    public List<String> getColor() {
        return color;
    }

    public void setColor(List<String> color) {
        this.color = color;
    }

    public Map<String, String> getBasedCity() {
        return basedCity;
    }

    public void setBasedCity(Map<String, String> basedCity) {
        this.basedCity = basedCity;
    }
}

创建Apple子类绑定apple节点:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "apple")
public class Apple extends Fruit {}

创建Banana子类绑定banana节点:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "banana")
public class Banana extends Fruit {}

方式2:直接注入不同配置(无需子类)

如果不想创建子类,也可以在配置类中手动注册两个Fruit实例:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FruitConfig {

    @Bean
    @ConfigurationProperties(prefix = "apple")
    public Fruit apple() {
        return new Fruit();
    }

    @Bean
    @ConfigurationProperties(prefix = "banana")
    public Fruit banana() {
        return new Fruit();
    }
}

步骤3:注入并使用配置

在需要使用的类中直接注入即可:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FruitService {

    private final Apple apple;
    private final Banana banana;

    // 构造器注入(推荐)
    @Autowired
    public FruitService(Apple apple, Banana banana) {
        this.apple = apple;
        this.banana = banana;
    }

    // 测试使用
    public void printFruitInfo() {
        System.out.println("Apple colors: " + apple.getColor());
        System.out.println("Apple based cities: " + apple.getBasedCity());
        System.out.println("Banana colors: " + banana.getColor());
        System.out.println("Banana based cities: " + banana.getBasedCity());
    }
}

关键说明

  1. @ConfigurationProperties会自动处理YAML中的下划线命名(based-city)与Java类的驼峰命名(basedCity)的映射。
  2. 必须为类属性提供Getter/Setter,或者使用@ConstructorBinding配合全参构造器(Spring Boot 2.2+支持)。
  3. 如果不使用@Component标记类,需要在启动类或配置类上添加@EnableConfigurationProperties({Apple.class, Banana.class})来启用配置绑定。

内容的提问来源于stack exchange,提问作者Varun Bansal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 01:43:24