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

Spring Boot中@ConfigurationProperties绑定的Map<String,User>如何在yaml赋值?

配置方法说明

你示例代码中的@ConfigurationProperties(config.value)属于笔误,正确写法需要指定配置前缀,以下内容基于前缀为config的写法示例,即注解为@ConfigurationProperties(prefix = "config")。

完整application.yaml配置示例

config:
  # 对应配置类中的userProperties属性,支持短横线命名和驼峰命名两种写法
  user-properties:
    # 此处的u1、u2为Map的自定义字符串key,可根据需求任意修改
    u1:
      # 对应User类的userName属性
      user-name: 张三
      # 对应User类的userCity属性
      user-city: 北京
    u2:
      user-name: 李四
      user-city: 上海

注意事项

  • 配置类需要被Spring识别绑定,要么在启动类/配置类上添加@EnableConfigurationProperties(ApplicationProperties.class)注解,要么在ApplicationProperties类上添加@Component注解交给Spring容器管理
  • Spring Boot默认支持驼峰命名和短横线分隔命名自动映射,userProperties可写为user-properties,userName可写为user-name,直接写原始驼峰名也可正常识别
  • Map的key没有格式限制,可根据业务需求自定义任意字符串作为key

内容的提问来源于stack exchange,提问作者Anuja Barve

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 21:18:03