yaml配置对象map

时间:2025-04-05 07:30:25

yaml配置如下:

objectConfig:
  object-map:
    1:
      name: "对象一"
      desc: "这是第一个对象"
      url: ""
    2:
      name: "对象二"
      desc: "这是第二个对象"
      url: ""

Java配置类代码如下:

@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "objectConfig")
public class ObjectConfig {
  private Map<Integer, ObjectEntity> objectMap;
}

  public static class ObjectEntity{
    private String name;
    private String desc;
    private String url;
  }
}

Java使用代码如下:

@Autowired
private ObjectConfig objectConfig;

ObjectConfig.ObjectEntity objectEntity = objectConfig.getObjectMap().get(1);