基础配置路径及其加载顺序

Springboot程序启动时,会从以下位置加载配置文件:

  • 1.file:./config/ :当前项目下的/config目录下

  • 2.file:./ :当前项目的根目录

  • 3.classpath:/config/ :classpath的/config目录

  • 4.classpath:/ :classpath的根目录

注意:在多个model的工程中,这种配置顺序不生效

外部配置文件加载顺序

  1. 开启 DevTools 时, 在根目录创建该文件:~/spring-boot-devtools.properties
  2. 在测试类上的 @TestPropertySource 注解
  3. @SpringBootTest里的properties 属性
  4. ==命令⾏参数(--server.port=9000 )==
  5. SPRING_APPLICATION_JSON 中的属性
  6. ServletConfig 初始化参数
  7. ServletContext 初始化参数
  8. java:comp/env 中的 JNDI 属性
  9. System.getProperties()
  10. 操作系统环境变量
  11. random.* 涉及到的 RandomValuePropertySource
  12. jar 包外部的 application-{profile}.properties 或 .yml
  13. jar 包内部的 application-{profile}.properties 或 .yml
  14. jar 包外部的 application.properties 或 .yml
  15. jar 包内部的 application.properties 或 .yml
  16. @Configuration 类上的 @PropertySource
  17. SpringApplication.setDefaultProperties() 设置的默认属性