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

Spring Boot如何将配置读取为Map并解决Office字段为空问题

问题:Spring Boot配置绑定中Map内嵌Office对象为null的解决方法

我有一个对接配置服务器的服务,会从该服务器拉取配置文件。以下是配置文件的简化示例:

car:
    #name of Map
    model:
        volkswagen:
            year: 1937
            ranking: 2
            office:
                country: germany
                city: wolfsburg
            
        honda:
            year: 1948
            ranking: 3
            office:
                country: japan
                city: tokyo
    #common parameters for all models
    wheels: 4
    #etc...

我尝试通过以下Java类实现配置绑定为Map:

Office实体类

public class Office
{
    private String country;
    
    private String city;
    
    //setters and getters
}

CarModel实体类

public class CarModel
{
    private Office office;
    
    private String year;
    
    private int ranking;

    //setters and getters
}

Car配置类

@Configuration
@ConfigurationProperties(prefix = "car")
public class Car
{
    private Map<String, CarModel> model = new HashMap<>();
    
    private int wheels;
    //other common fields and setters and getters for them
    
    
    public Map<String, CarModel> getModel()
    {
        return model;
    }
}

当我在@Service类中注入Car并使用时,所有公共字段(如wheels等)都有值,Map中每个key对应的year和ranking也有值,但office字段为null。请问如何在不修改yaml文件结构且不使用Environment手动读取配置的情况下解决这个问题?


解决方案

1. 确保嵌套类的Setter方法为public

Spring Boot的配置绑定依赖公共的Setter方法来注入嵌套对象的属性,你需要确保:

  • Office类的country、city字段都有public的Setter
  • CarModel类的office字段有public的Setter

修正后的Office类:

public class Office {
    private String country;
    private String city;

    // 公共Setter方法
    public void setCountry(String country) {
        this.country = country;
    }

    public void setCity(String city) {
        this.city = city;
    }

    // Getter方法(可选,但建议保留)
    public String getCountry() {
        return country;
    }

    public String getCity() {
        return city;
    }
}

修正后的CarModel类:

public class CarModel {
    private Office office;
    private String year;
    private int ranking;

    // office字段的公共Setter
    public void setOffice(Office office) {
        this.office = office;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    // Getter方法
    public Office getOffice() {
        return office;
    }

    public String getYear() {
        return year;
    }

    public int getRanking() {
        return ranking;
    }
}

2. (可选)添加配置处理器依赖

如果项目中未引入spring-boot-configuration-processor,建议添加该依赖以生成配置元数据,帮助Spring更准确地完成绑定(Maven示例):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

完成以上修改后,Spring就能正确将配置中的office子属性注入到CarModel的office实例中。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 04:27:34