今日遇到一个问题,SpringBoot无法读取到application.yml中的属性。
只有经历挫折才能记忆深刻!忽略了一个细节:yml 语法格式中,对象格式为key: value。冒号后面要加一个空格!
application.yml配置内容如下
com: tom: servicetime: starthour:21 endhour:24PropertiesConfigure.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一定要注意细节!