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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值