springboot项目中的bootstrap.yml配置不生产(没有自动提示也没有变绿叶)
1、背景与现象:
(1)新创建一个 springboot项目,添加了 bootstrap.yml 文件,发现文件并没有如预期变成绿色叶子,编写的时候也没有自动提示。
(2)启动的时候,发现端口是8080,并没有如配置上写的端口进行启动——由此发现配置并没有生效,所以是为什么呢。。。
2、问题定位
为什么不生效呢,通过资料查找,原来:
(1)单纯的 springboot 项目只会识别 application.* 配置文件, bootstrap.yml 它不认识啊,所以并没有被识别为配置文件,所以也不会变成绿叶,也没有自动提示了。——对应方法1 : 把配置文件改为 application.*
(2)如果是 springcloud 项目,就能够识别 bootstrap.yml | bootstrap.properties,相同配置以 bootstrap.yml 为准。如果还有 application文件, bootstrap 优先于 application 文件(以 application 为准)
(3)那我刚才建的项目,它是一个 springboot项目,还不是一个springcloud项目,两者要怎样去切换呢?
添加一个依赖,就把 springboot 变成 springcloud 了
org.springframework.cloud
spring-cloud-starter-bootstrap
3、bootstrap.yml, bootstrap.properties, application.yml, application.properties 优先级哪个高,以哪个为准?
加载顺序(优先级): bootstrap > application > 配置中心
bootstrap.yml > bootstrap.properties > application.yml > application.properties
后者覆盖前者
所以,最终,是以配置中心为准?emm