疑问
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