springboot配置文件加载顺序

配置文件覆盖顺序:(加载顺序从1->9,但在之前会确定用哪个activeProfiles,覆盖顺序1-9)
(1)默认配置:defaultProfiles(默认配置,springboot源代码提供的)
(2)属性配置:propertySources(写在代码中注解上的)
(3)Servlet初始化参数配置:servletConfigInitParams(配置给servlet全局的)
(4)Servlet全局上下文配置:servletContextInitParams(配置给servlet上下文的)
(5)系统配置配置:systemProperties(system.setProperty()方法配置的)
(6)系统环境配置:systemEnvironment(例如:classpath,path这种系统级别的)
(7)随机值配置:random(springboot随机值生成器,配置文件中${random.xxx}配置)
(8)系统环境配置(内):applicationConfig(内,覆盖顺序为:java -jar -Dspring.config.location=指定文件、./config/目录下、./目录下、classpath下)
(9)系统环境配置(外):applicationConfig(外,覆盖顺序同上)

备注:若要在jar包外使用application.yml或application.properties的配置,activeProfiles值一定要在外配置,且不能是空字符串,即如下图,否则该值会用jar包内的值

例如:输出org.springframework.core.env.Environment对象可知
StandardServletEnvironment {
  activeProfiles=[nanning],
  defaultProfiles=[default],
  propertySources=[
    StubPropertySource {name='servletConfigInitParams'},
    StubPropertySource {name='servletContextInitParams'},
    MapPropertySource {name='systemProperties'},
    SystemEnvironmentPropertySource {name='systemEnvironment'},
    RandomValuePropertySource {name='random'},
    MapPropertySource {name='applicationConfig: [classpath:/application-nanning.yml]'},
    MapPropertySource {name='applicationConfig: [file:config/application.yml]'},
    MapPropertySource {name='applicationConfig: [file:./config/application.yml]'},
    MapPropertySource {name='applicationConfig: [classpath:/application.yml]'}
  ]
}

更详细参见

  https://www.cnblogs.com/softidea/p/5759180.html

原文地址:https://www.cnblogs.com/chendeming/p/8334099.html