首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >关于springboot配置文件路径优先级

关于springboot配置文件路径优先级

作者头像
冬天里的懒猫
发布2021-02-04 10:44:56
发布2021-02-04 10:44:56
1K0
举报

在springboot项目中,最重要的配置文件是application.yaml文件。然而这个文件究竟应该放置在什么位置呢?在实际操作的过程中,配置文件可以存在的路径如下表:

配置文件位置

说明

file:./config/

为于project目录下的config目录,实际上对应于jar文件同一目录的config目录。

file:./

projet目录。实际上对应于jar文件的同一目录。

classpath:/config/

jar包内的文件目录,对应代码的resource目录中的config

classpath:/

jar包内的文件,对应代码的resource目录。

上述4个路径都能放置application.yaml文件,那么现在我们来测试上述4个位置的优先级。 以MySpringBoot项目为例:

我们将上述4个配置文件的server port分别配置为 8081、8082、8083、8084。

代码语言:javascript
复制
server:
  port: 8081

之后我们启动springboot项目:

代码语言:javascript
复制
2021-02-02 14:49:03.491  INFO 16724 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-02-02 14:49:03.514  INFO 16724 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2021-02-02 14:49:03.521  INFO 16724 --- [  restartedMain] com.dhb.MySpringBootApplication          : Started MySpringBootApplication in 1.38 seconds (JVM running for 2.044)

可以看到,springboot实际上监听的是8081端口。 说明file:./config/ 的优先级是最高的。 之后我们将这个位置对应的配置文件删除再启动:

代码语言:javascript
复制
2021-02-02 14:50:43.153  INFO 18276 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-02-02 14:50:43.175  INFO 18276 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2021-02-02 14:50:43.181  INFO 18276 --- [  restartedMain] com.dhb.MySpringBootApplication          : Started MySpringBootApplication in 1.406 seconds (JVM running for 2.013)

可以看到此时的配置文件,生效的是file:./ 下面的配置文件,此时的监听端口为8082。 我们再删除此文件之后启动:

代码语言:javascript
复制
2021-02-02 14:52:40.730  INFO 12004 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-02-02 14:52:40.755  INFO 12004 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path ''
2021-02-02 14:52:40.762  INFO 12004 --- [  restartedMain] com.dhb.MySpringBootApplication          : Started MySpringBootApplication in 1.409 seconds (JVM running for 2.031)

此时监听的端口是8083。 说明在classpath中的优先级,config目录下的优先级高于config目录之外的配置文件。 同样,我们再将此文件删除,重启,此时监听端口会变为8084。

代码语言:javascript
复制
2021-02-02 14:54:19.761  INFO 16200 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-02-02 14:54:19.784  INFO 16200 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8084 (http) with context path ''
2021-02-02 14:54:19.793  INFO 16200 --- [  restartedMain] com.dhb.MySpringBootApplication          : Started MySpringBootApplication in 1.392 seconds (JVM running for 2.011)

这样我们得到了springboot配置文件路径的优先级,file > classpath, 有config目录>无config目录。

配置文件位置

优先级顺序

file:./config/

最高,1

file:./

其次,2

classpath:/config/

第三,3

classpath:/

最低,4

这样我们再使用springboot的时候,就很清楚配置文件应该存在于什么位置是最合适。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档