今天在了解springboot starter时,写了一个starter的demo ,在starter中使用 ConfigurationProperties 注解配置类时,发现读取不到application.yml配置文件中的配置,后来将 application.yml改为 application.properties,以后就能正常读取了。
HelloProperties.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String msg;
}
application.properties
hello.msg: hello world starter
application.yml
hello:
msg: hello world starter
demo 项目为
https://gitee.com/j__domino/springboot-demo.git 中的starter模块
问题原因应该是 以下两个maven依赖的版本的问题,这个项目是下面代码中的版本,读yml有问题,读properties正常
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.0.RELEASE</version>
<optional>true</optional>
</dependency>
而我的另外一个项目,依赖的版本是 2.1.7.RELEASE,就变成了读yml正常,但是读properties不正常了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>