实战《Spring Boot 2.1.5》-配置文件覆盖优先级以及方式

疑问

SpringBoot项目,使用java -jar命令启动时,配置文件的优先级如何?配置文件覆盖方式是整个文件还是单个属性值?

结果

文件类型

application[-profile].application

ymal格式应该也是一样,不过没有测试

覆盖方式

属性值覆盖

优先级

当前目录执行

jar包所在目录:D:\springboot,启动命令:D:\springboot>java -jar xxx,优先级如下:

启动命令指定配置 > file:/config > file:/ > classpath:/config > classpath:/

非当前目录执行

jar包所在目录:D:\springboot,启动命令:D:>java -jar xxx,优先级如下:

启动命令指定配置 > 当前目录:/config > 当前目录:/ > classpath:/config > classpath:/

代码测试

classpath:/

application.properties

spring.profiles.active=dev
server.port=8080

application-dev.properties

server.port=8081
value=classpath-value

运行效果

  • 端口:8081
  • value:classpath-value
D:\springboot>java -jar migrator-1.0.0.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.5.RELEASE)

2020-03-15 16:46:31.812  INFO 9444 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 9444 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\springboot)
2020-03-15 16:46:31.815  INFO 9444 --- [           main] org.fjad.study.MigratorApplication       : The following profiles are active: dev
2020-03-15 16:46:33.149  INFO 9444 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2020-03-15 16:46:33.178  INFO 9444 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 16:46:33.179  INFO 9444 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 16:46:33.278  INFO 9444 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 16:46:33.279  INFO 9444 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1419 ms
2020-03-15 16:46:33.467  INFO 9444 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 16:46:33.647  INFO 9444 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2020-03-15 16:46:33.650  INFO 9444 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.207 seconds (JVM running for 2.541)
classpath-value

增加classpath:/config/application-dev.properties

jar包内的目录关系:

..\config\application-dev.properties
..\application-dev.properties

…\config\application-dev.properties

value=classpath-config-value
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑾析编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值