spring-boot-demo icon indicating copy to clipboard operation
spring-boot-demo copied to clipboard

180919-SpringBoot基础篇配置信息之如何读取配置信息 | 一灰灰Blog

Open liuyueyi opened this issue 4 years ago • 1 comments

http://spring.hhui.top/spring-blog/2018/09/19/180919-SpringBoot%E5%9F%BA%E7%A1%80%E7%AF%87%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF%E4%B9%8B%E5%A6%82%E4%BD%95%E8%AF%BB%E5%8F%96%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF/

SpringBoot极大的减少了配置,开一个新项目时,完全可以做到什么配置都不加,就可以直接跑,简单方便的同时,就带来了一个问题 怎么知道这些默认的配置是什么? 如果要修改默认配置怎么办? 如何添加自定义的配置? 如何读取这些配置?

liuyueyi avatar Aug 06 '19 03:08 liuyueyi

@Value 默认情况下要求映射的配置存在;若不存在,在启动时会抛异常,如 Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx' in value "${xxx}"

如果希望避免出现上面的情况,可以通过设置默认值的方式来实现

@Value(xxx:) 冒号后面的就是默认值

liuyueyi avatar Jun 02 '21 06:06 liuyueyi