SpringBoot: 配置加载顺序
在应用程序中各种配置是不可避免的,Spring中对配置的抽象(Environment)可以说是达到了极致,其中有一项尤为突出:PropertySource(配置来源),配置来源通常包括命令行参数,系统属性,系统变量,perperties文件等。在使用SpringBoot过程中,将这些技术更进一步发挥,已经可以在很多环境下使用各种各样的配置。
配置的使用通常使用${}占位符来表示,可以在 properties文件中,基于XML的applicationContext.xml配置中,基于Annotation的@Value、@ConfigurationProperies中使用。
那么在使用过程中,查找顺序配置项的如何呢?
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). 用于开发环境。 @TestPropertySource
annotations on your tests. (用于单元测试环境)@SpringBootTest#properties
annotation attribute on your tests.(用于单元测试环境)- Command line arguments. (命令行参数 java -jar xxx.jar args, 其中参数形式是 --key1=value1 --key2=value2)
- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property) ServletConfig
init parameters.ServletContext
init parameters.- JNDI attributes from
java:comp/env
. - Java System properties (
System.getProperties()
). - OS environment variables.
- A
RandomValuePropertySource
that only has properties inrandom.*
. (这个是用于取一个随机数,例如 random.int, random.long, random.uuid, random.int(10), random.int[10,100]等等) - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants) 其实包括 .properties,.xml,.yml,yaml,详情见下面说明。 - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants) - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). @PropertySource
annotations on your@Configuration
classes.- Default properties (specified using
SpringApplication.setDefaultProperties
).
对于第12, 13, 14, 15 项,详细情况是这样的:
在程序启动是,会查找并加载Profiles文件,它的详细过程是:
1) 取得要激活哪些profiles,以及这些profile可能存放的位置。
1.1) 使用PropertySources.get("spring.profiles.active")获取到要激活哪些Profiles,如果不指定会使用默认的名字:application。
当然了,也可以使用编程的方式设置: 在程序开始之前执行代码:SpringApplication.setAdditionalProfiles("yourProfile")
1.2) 使用PropertySources.get("spring.config.location")获取要激活的profiles可能存放的位置。如果不指定该属性,会使用默认位置:
file:./config/ file:./ classpath:./config/ classpath:./
2) 从"spring.config.location指定的位置去加载"spring.profiles.active指定的配置文件。
配置文件以 [.properties, .xml, .yml, yaml]顺序查找,找不到的情况下会找下一个。
examples:
如果没有配置spring.profiles.active, spring.config.location, 则会找
file:./config/application-default.[peroperties|xml|yml|yaml] file:./application-default.[peroperties|xml|yml|yaml] classpath:./config/application-default.[peroperties|xml|yml|yaml] classpath:./application-default.[peroperties|xml|yml|yaml] file:./config/application.[peroperties|xml|yml|yaml] file:./application.[peroperties|xml|yml|yaml] classpath:./config/application.[peroperties|xml|yml|yaml] classpath:./application.[peroperties|xml|yml|yaml]
SpringBoot: 配置加载顺序的更多相关文章
- SpringBoot配置加载顺序
一般我们会将SpringBoot应用需要的配置内容放在项目工程中,然后通过spring.profiles.active或是通过Maven来实现多环境的支持.但是,当团队逐渐壮大,分工越来越细之后,往往 ...
- 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 ...
- SpringBoot——配置文件加载位置及外部配置加载顺序
声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...
- Spring Boot 配置加载顺序详解
使用 Spring Boot 会涉及到各种各样的配置,如开发.测试.线上就至少 3 套配置信息了.Spring Boot 可以轻松的帮助我们使用相同的代码就能使开发.测试.线上环境使用不同的配置. 在 ...
- Spring Boot配置加载顺序
如果加载的配置有重复的,它们的加载顺序是这样的,数字越小的优先级越高,即优先级高的覆盖优先级低的配置. Devtools global settings properties on your home ...
- 【串线篇】spring boot外部配置加载顺序
SpringBoot也可以从以下位置加载配置: 原则仍然是优先级从高到低:高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 java -j ...
- web.xml 配置 加载顺序
web.xml 的加载顺序是:context-param -> listener -> filter -> servlet . 过滤器执行顺序是根据filter-mapping ,不 ...
- deepin 环境变量配置加载顺序
加载顺序,默认是没有 .bash_profile 文件 1. non-login 方式 ~$ su deepin run /ect/bash.bashrc run .bashrc 2.login 方式 ...
- Springboot配置文件加载顺序
使用Springboot开发的时候遇到了配置的问题,外部config里的配置文件本来没有配置https怎么启动还是https呢,原来开发中测试https在classpath路径的配置文件添加https ...
随机推荐
- 源码实现 --> strcpy
拷贝字符串到目标字符串 函数 char *strcpy(char *strDestination, const char *strSource); 复制源串strSource到目标串strDestin ...
- [W班]第二次结对作业成绩评价
作业地址: https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1016 作业要求: 1.代码具有规范性. ...
- Beta Scrum Day 4
听说
- MySQL的小Tips
交集和差集 MySQL中没有这两个运算,但是有并集运算,所以可以利用这个来间接实现. 差集: SELECT ID FROM ( SELECT DISTINCT A.AID AS ID FROM TAB ...
- Android Studio使用过程中遇到的错误
> 错误1 1. This fragment should provide a default constructor (a public constructor wit 代码不规范,这个错误是 ...
- nyoj 星期几?
星期几? 时间限制:500 ms | 内存限制:65535 KB 难度:2 描述 Acmer 小鱼儿 埋头ku算一道题 条件:已知给定 一日期 告诉你 ...
- python之路--day10-闭包函数
1.命名关键字参数 格式:在*后面的参数都是命名关键字参数 特点: 1.必须被传值 2.约束函数的调用者必须按照key=value的形式传值 3.约束函数的调用者必须用我们指定的key名 def au ...
- CentOS 7 Redis安装配置
1.获取Redis压缩包: wget http:.tar.gz 2.解压测试: mv 到 /usr/local/ tar .tar cd redis 3.使用make测试编译: make 这里可能会出 ...
- 剑指offer-两个链表的第一个公共节点
题目描述 输入两个链表,找出它们的第一个公共结点. 解题思路 分析可得如果两个链表有公共节点,那么公共节点出现在两个链表的尾部,即从某一节点开始,两链表之后的节点全部相等.可以首先遍历两个链表得出各自 ...
- Python内置函数(10)——float
英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...