在spring boot中,需要取配置文件的值可以通过注解@Value(“${}”)来取值,但是有时候会取不到值,因为踩中了一些陷阱。今天讲的取值默认是取application.properties文件里面的值,这是spring boot默认配置文件。
- 读取配置文件值的类通过new 方式生成
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-17sbOKSl-1650612174844)(http://p3.pstatp.com/large/402d0002576cdd18407e)]
文件读取类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DxmU5Rkc-1650612174845)(http://p3.pstatp.com/large/402f0000f9900a55c342)]
new取不到值
正确做法:取文件的类通过spring的autowired或者resource取值
2.读取配置文件的值用static修饰
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3u1r5f4u-1650612174846)(http://p3.pstatp.com/large/402b00026b63693edb27)]
static修饰
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qbrS5Rl7-1650612174846)(http://p3.pstatp.com/large/402d000257bfbdc90efb)]
static修饰属性取不到值
正确做法:通过value取值的属性名不可以为static
c



