實戰《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

運行效果

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

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

2020-03-15 16:49:34.212  INFO 100 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 100 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\springboot)
2020-03-15 16:49:34.214  INFO 100 --- [           main] org.fjad.study.MigratorApplication       : The following profiles are active: dev
2020-03-15 16:49:35.595  INFO 100 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2020-03-15 16:49:35.622  INFO 100 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 16:49:35.623  INFO 100 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 16:49:35.721  INFO 100 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 16:49:35.721  INFO 100 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1456 ms
2020-03-15 16:49:35.955  INFO 100 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 16:49:36.143  INFO 100 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2020-03-15 16:49:36.147  INFO 100 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.285 seconds (JVM running for 2.601)
classpath-config-value

增加file:/application-dev.properties

目錄關係:

D:\springboot\migrator-1.0.0.jar
D:\springboot\application-dev.properties

application-dev.properties

server.port=8082
value=file-value

運行效果

  • 端口:8082
  • value:file-dev
D:\springboot>java -jar migrator-1.0.0.jar

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

2020-03-15 16:51:01.316  INFO 11168 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 11168 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\springboot)
2020-03-15 16:51:01.319  INFO 11168 --- [           main] org.fjad.study.MigratorApplication       : The following profiles are active: dev
2020-03-15 16:51:02.649  INFO 11168 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2020-03-15 16:51:02.677  INFO 11168 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 16:51:02.678  INFO 11168 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 16:51:02.771  INFO 11168 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 16:51:02.772  INFO 11168 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1408 ms
2020-03-15 16:51:02.971  INFO 11168 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 16:51:03.140  INFO 11168 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2020-03-15 16:51:03.143  INFO 11168 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.176 seconds (JVM running for 2.495)
file-value

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

目錄關係:

D:\springboot\migrator-1.0.0.jar
D:\springboot\application-dev.properties
D:\springboot\config\application-dev.properties

D:\springboot\config\application-dev.properties

server.port=8083

運行效果

  • 端口:8083
  • value:file-value
D:\springboot>java -jar migrator-1.0.0.jar

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

2020-03-15 16:54:47.296  INFO 8984 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 8984 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\springboot)
2020-03-15 16:54:47.298  INFO 8984 --- [           main] org.fjad.study.MigratorApplication       : The following profiles are active: dev
2020-03-15 16:54:48.574  INFO 8984 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8083 (http)
2020-03-15 16:54:48.597  INFO 8984 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 16:54:48.598  INFO 8984 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 16:54:48.687  INFO 8984 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 16:54:48.687  INFO 8984 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1346 ms
2020-03-15 16:54:48.892  INFO 8984 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 16:54:49.053  INFO 8984 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path ''
2020-03-15 16:54:49.056  INFO 8984 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.129 seconds (JVM running for 2.442)
file-value

啓動命令增加指定配置

啓動命令:java -Dspring.config.location=my-application.properties -jar migrator-1.0.0.jar

my-application.properties

server.port=8084
value=my-value

運行效果

  • 端口:8084
  • value:my-value
D:\springboot>java -Dspring.config.location=my-application.properties -jar migrator-1.0.0.jar

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

2020-03-15 17:09:03.525  INFO 20384 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 20384 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\springboot)
2020-03-15 17:09:03.529  INFO 20384 --- [           main] org.fjad.study.MigratorApplication       : No active profile set, falling back to default profiles: default
2020-03-15 17:09:04.844  INFO 20384 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8084 (http)
2020-03-15 17:09:04.874  INFO 20384 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 17:09:04.874  INFO 20384 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 17:09:04.969  INFO 20384 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 17:09:04.969  INFO 20384 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1394 ms
2020-03-15 17:09:05.171  INFO 20384 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 17:09:05.342  INFO 20384 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8084 (http) with context path ''
2020-03-15 17:09:05.345  INFO 20384 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.137 seconds (JVM running for 2.453)
my-value

非當前目錄執行

當前路徑爲D:\,啓動命令:java -jar springboot/migrator-1.0.0.jar

運行效果

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

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

2020-03-15 17:16:39.301  INFO 17180 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 17180 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\)
2020-03-15 17:16:39.303  INFO 17180 --- [           main] org.fjad.study.MigratorApplication       : The following profiles are active: dev
2020-03-15 17:16:40.609  INFO 17180 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2020-03-15 17:16:40.639  INFO 17180 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 17:16:40.640  INFO 17180 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 17:16:40.728  INFO 17180 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 17:16:40.728  INFO 17180 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1380 ms
2020-03-15 17:16:40.937  INFO 17180 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 17:16:41.113  INFO 17180 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2020-03-15 17:16:41.116  INFO 17180 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.158 seconds (JVM running for 2.475)
classpath-config-value

非當前目錄執行,執行目錄下有配置文件

當前路徑爲D:\,啓動命令:java -jar springboot/migrator-1.0.0.jar

D:\application-dev.properties

server.port=8085
value=current-file-value

運行效果

  • 端口:8085
  • value:current-file-value
D:\>java -jar springboot/migrator-1.0.0.jar

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

2020-03-15 17:33:05.250  INFO 11760 --- [           main] org.fjad.study.MigratorApplication       : Starting MigratorApplication v1.0.0 on HGYdePC with PID 11760 (D:\springboot\migrator-1.0.0.jar started by guoyu.huang in D:\)
2020-03-15 17:33:05.253  INFO 11760 --- [           main] org.fjad.study.MigratorApplication       : The following profiles are active: dev
2020-03-15 17:33:06.529  INFO 11760 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8085 (http)
2020-03-15 17:33:06.557  INFO 11760 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-15 17:33:06.558  INFO 11760 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2020-03-15 17:33:06.640  INFO 11760 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-15 17:33:06.640  INFO 11760 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1344 ms
2020-03-15 17:33:06.840  INFO 11760 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-15 17:33:07.014  INFO 11760 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8085 (http) with context path ''
2020-03-15 17:33:07.017  INFO 11760 --- [           main] org.fjad.study.MigratorApplication       : Started MigratorApplication in 2.122 seconds (JVM running for 2.434)
current-file-value

代碼解析

// TODO

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章