在spring boot中,需要取配置文件的值可以通过注解@Value("${}")来取值,但是有时候会取不到值,因为踩中了一些陷阱。今天讲的取值默认是取application.properties文件里面的值,这是spring boot默认配置文件。

  1. 读取配置文件值的类通过new 方式生成

文件读取类

new取不到值

正确做法:取文件的类通过spring的autowired或者resource取值

2.读取配置文件的值用static修饰

static修饰

static修饰属性取不到值

正确做法:通过value取值的属性名不可以为static

更多推荐

spring boot @Value取值