配置文件加载位置

加载优先级
假定项目路径为project/,
SpringBoot启动会扫描以下位置的application.properties或者application.yml文件作为SpringBoot的默认配置文件

  • /project/config/
  • /project
  • classpath:/config/
  • classpath:/

优先级从高到低,所有配置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。

另外我们可以通过配置spring.config.location来改变默认配置,用于项目打包以后,使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用,形成互补配置。

spring.config.location测试
在classpath:/config下新建application.yml:

  1. server:
  2. port: 3333

在K盘下新建application.yml:

  1. server:
  2. port: 5555
  3. admin:
  4. name: Khighness

编写controller:

  1. @RestController
  2. public class HelloController {
  3. @Value("${admin.name}")
  4. public String adminName;
  5. @GetMapping("/name")
  6. public String hello() {
  7. return adminName;
  8. }
  9. }

在项目根路径下运行如下命令:

  1. $ mvn apckage
  2. $ cd target
  3. $ java -jar springboot-basic-1.0-SNAPSHOT.jar --spring.config.location=K:/application.yml

运行结果:

  1. ██╗ ██╗██╗ ██╗██╗ ██████╗ ██╗ ██╗███╗ ██╗███████╗███████╗███████╗
  2. ██║ ██╔╝██║ ██║██║██╔════╝ ██║ ██║████╗ ██║██╔════╝██╔════╝██╔════╝
  3. █████╔╝ ███████║██║██║ ███╗███████║██╔██╗ ██║█████╗ ███████╗███████╗
  4. ██╔═██╗ ██╔══██║██║██║ ██║██╔══██║██║╚██╗██║██╔══╝ ╚════██║╚════██║
  5. ██║ ██╗██║ ██║██║╚██████╔╝██║ ██║██║ ╚████║███████╗███████║███████║
  6. ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚══════╝╚══════╝
  7. 2021-04-14 22:41:21.494 INFO 1208 --- [ main] top.parak.KhighnessApplication : Starting KhighnessApplication v1.0-SNAPSHOT on KHighness-Dell with PID 1208 (D:\Java\springboot\springboot-basic\target\springboot-basic-1.0-SNAPSHOT.jar started by 18236 in D:\Java\springboot\springboot-basic\target)
  8. 2021-04-14 22:41:21.497 INFO 1208 --- [ main] top.parak.KhighnessApplication
  9. : No active profile set, falling back to default profiles: default
  10. 2021-04-14 22:41:22.344 INFO 1208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 5555 (http)
  11. 2021-04-14 22:41:22.354 INFO 1208 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  12. 2021-04-14 22:41:22.354 INFO 1208 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
  13. 2021-04-14 22:41:22.355 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
  14. 2021-04-14 22:41:22.356 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
  15. 2021-04-14 22:41:22.356 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
  16. 2021-04-14 22:41:22.358 INFO 1208 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1c 28 May 2019]
  17. 2021-04-14 22:41:22.409 INFO 1208 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
  18. 2021-04-14 22:41:22.409 INFO 1208 --- [ main] o.s.web.context.ContextLoader
  19. 2021-04-14 22:41:22.560 INFO 1208 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  20. 2021-04-14 22:41:22.634 INFO 1208 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [public/index.html]
  21. 2021-04-14 22:41:22.716 INFO 1208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 5555 (http) with context path ''
  22. 2021-04-14 22:41:22.718 INFO 1208 --- [ main] top.parak.KhighnessApplication : Started KhighnessApplication in 1.526 seconds (JVM running for 1.819)

接口测试:

  1. $ curl http://localhost:5555/name
  2. StatusCode : 200
  3. StatusDescription :
  4. Content : Khighness
  5. RawContent : HTTP/1.1 200
  6. Keep-Alive: timeout=60
  7. Connection: keep-alive
  8. Content-Length: 9
  9. Content-Type: text/plain;charset=UTF-8
  10. Date: Wed, 14 Apr 2021 14:46:15 GMT
  11. Khighness
  12. Forms : {}
  13. Headers : {[Keep-Alive, timeout=60], [Connection, keep-alive], [Content-Length, 9], [Content-Type, text/plain;charset=UTF-8]...}
  14. Images : {}
  15. InputFields : {}
  16. Links : {}
  17. ParsedHtml : mshtml.HTMLDocumentClass
  18. RawContentLength : 9

外部配置加载顺序

官方文档
📖 boot-features-external-config

官方文档列举了17种:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. [@TestPropertySource](https://docs.spring.io/spring/docs/4.3.13.RELEASE/javadoc-api/org/springframework/test/context/TestPropertySource.html) annotations on your tests.
  3. [@SpringBootTest#properties](https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/api/org/springframework/boot/test/context/SpringBootTest.html) annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that only has properties in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. [@PropertySource](https://docs.spring.io/spring/docs/4.3.13.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html) annotations on your @Configuration classes.
  17. Default properties (specified using SpringApplication.setDefaultProperties).

比较重要
SpringBoot从以下位置加载配置,按照优先级从上至下;
高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置。
(1)命令行参数(java -jar =
(2)来自java:comp/env是JNDI属性
(3)Java系统属性(System.getProperties)
(4)操作系统环境变量
(5)RandomValuePropertySource配置random.*属性值
(6)jar包外部的application-{profilr}.properties或application.yml(带spring.profile)配置文件
(7)jar包内部的application-{profilr}.properties或application.yml(带spring.profile)配置文件
(8)jar包外部的applicatio.properties或application.yml(不带spring.profile)配置文件
(9)jar包内部的applicatio.properties或application.yml(不带spring.profile)配置文件
(10)@Configuration注解类上的@PropertySource

总结
最先加载命令行和环境属性;
优先加载带profile,再加载不带profile,由jar外至jar包内;
最后加载注解。