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

在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项目为例:
image.png

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

server:
  port: 8081

之后我们启动springboot项目:

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/ 的优先级是最高的。
之后我们将这个位置对应的配置文件删除再启动:

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。
我们再删除此文件之后启动:

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。

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的时候,就很清楚配置文件应该存在于什么位置是最合适。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值