SpringBoot 读取yml配置信息失败的一个细节性问题

xiaoxiao2025-05-22  48

今日遇到一个问题,SpringBoot无法读取到application.yml中的属性。

只有经历挫折才能记忆深刻!忽略了一个细节:yml 语法格式中,对象格式为key: value。冒号后面要加一个空格!

application.yml配置内容如下

com: tom: servicetime: starthour:21 endhour:24

PropertiesConfigure.java

package com.tom.config.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class PropertiesConfigure { @Bean @ConfigurationProperties("com.tom.servicetime") SystemServiceProperty systemServiceProperty() { return new SystemServiceProperty(); } }

SystemServiceProperty.java

package com.tom.config.properties; import lombok.Data; @Data public class SystemServiceProperty { private int starthour; private int endhour; }

使用过程中,发现SystemServiceProperty bean的starthour、endhour 属性的值没有正常注入。

排查问题时候,发现是yml 语法格式的问题,在application.yml文件的配置修改后,结果正常。

com: tom: servicetime: starthour: 21 endhour: 24

一定要注意细节!

 

 

转载请注明原文地址: https://www.6miu.com/read-5030494.html

最新回复(0)