【问题标题】:unable to bind list of object properties from application.yml in spring无法在 spring 中绑定 application.yml 中的对象属性列表
【发布时间】:2022-07-01 21:22:38
【问题描述】:

我想将对象属性列表绑定到spring bean中的一个字段,但是spring没有绑定它。我错过了什么?我的环境是 SpringBoot v2.7.1 + Java 8。

application.yml

application:
  mappings:
    -   oldname: 'old name 1'
        newname: 'new name 1'
    -   oldname: 'old name 2'
        newname: 'new name 2'

MappingProperties.java

@ConfigurationProperties(prefix = "application")
public class MappingProperties {
    private List<Mapping> mappings;

    public List<Mapping> getServers() {
        return mappings;
    }

    public void setServers(List<Mapping> mappings) {
        this.mappings = mappings;
    }

    public class Mapping {
        private String oldname;
        private String newname;

        public String getOldname() {
            return oldname;
        }

        public void setOldname(String oldname) {
            this.oldname = oldname;
        }

        public String getNewname() {
            return newname;
        }

        public void setNewname(String newname) {
            this.newname = newname;
        }
    }
}

DemoApplication.java

@EnableConfigurationProperties(MappingProperties.class)
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private MappingProperties mappingProperties;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(mappingProperties);
    }
}

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    您好像不见了@ConfigurationPropertiesScan https://www.baeldung.com/configuration-properties-in-spring-boot

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      相关资源
      最近更新 更多