简体   繁体   English

无法从 spring 中的 application.yml 绑定 object 属性列表

[英]unable to bind list of object properties from application.yml in spring

I want to bind a list of object properties to a field in spring bean, but spring does not bind it.我想将 object 属性列表绑定到 spring bean 中的字段,但 spring 不绑定它。 What am I missing?我错过了什么? My env is SpringBoot v2.7.1 + Java 8.我的环境是 SpringBoot v2.7.1 + Java 8。

application.yml应用程序.yml

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

MappingProperties.java映射属性.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 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); } }

Looks like you are missing @ConfigurationPropertiesScan https://www.baeldung.com/configuration-properties-in-spring-boot看起来您缺少@ConfigurationPropertiesScan https://www.baeldung.com/configuration-properties-in-spring-boot

UPD: an implementation example was found here: https://www.baeldung.com/spring-boot-yaml-list UPD:在这里找到了一个实现示例: https://www.baeldung.com/spring-boot-yaml-list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 application.yml 读取属性时忽略 Spring 配置文件 - Spring profile is ignored when reading properties from application.yml "&#39;application.yml&#39; 中的 Spring Boot 属性未从 JUnit 测试加载" - Spring Boot properties in 'application.yml' not loading from JUnit Test Spring 的数据库 application.yml 从 applications.properties 启动 - Database application.yml for Spring boot from applications.properties 根据application.yml的属性注入Spring Boot - Inject with Spring Boot depending on properties from application.yml 无法从 Java Spring Boot 项目的 application.yml 文件中读取用户定义的类列表 - Unable to read list of user defined class from application.yml file in a Java Spring Boot project spring application.yml来自另一个属性的引用列表 - spring application.yml reference list from another property Spring Boot-无法读取Yaml属性文件application.yml - spring boot- unable to read yaml properties file application.yml 当我启动 Spring Boot 应用程序时,它没有从 application.yml 读取属性 - When I start my Spring boot application it is not reading properties from application.yml 如何在Spring 1.5.9中访问application.yml属性 - How to access application.yml properties in Spring 1.5.9 spring boot application.yml属性在运行时更改 - spring boot application.yml properties change at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM