在应用程序中各种配置是不可避免的,Spring中对配置的抽象(Environment)可以说是达到了极致,其中有一项尤为突出:PropertySource(配置来源),配置来源通常包括命令行参数,系统属性,系统变量,perperties文件等。在使用SpringBoot过程中,将这些技术更进一步发挥,已经可以在很多环境下使用各种各样的配置。

配置的使用通常使用${}占位符来表示,可以在 properties文件中,基于XML的applicationContext.xml配置中,基于Annotation的@Value、@ConfigurationProperies中使用。

那么在使用过程中,查找顺序配置项的如何呢?

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).  用于开发环境。
  2. @TestPropertySource annotations on your tests.         (用于单元测试环境)
  3. @SpringBootTest#properties annotation attribute on your tests.(用于单元测试环境)
  4. Command line arguments.  (命令行参数 java -jar xxx.jar args, 其中参数形式是 --key1=value1 --key2=value2
  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. RandomValuePropertySource that only has properties in random.*. (这个是用于取一个随机数,例如 random.int, random.long, random.uuid, random.int(10), random.int[10,100]等等)
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants) 其实包括 .properties,.xml,.yml,yaml,详情见下面说明。
  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 annotations on your @Configuration classes.
  17. 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: 配置加载顺序的更多相关文章

  1. SpringBoot配置加载顺序

    一般我们会将SpringBoot应用需要的配置内容放在项目工程中,然后通过spring.profiles.active或是通过Maven来实现多环境的支持.但是,当团队逐渐壮大,分工越来越细之后,往往 ...

  2. 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)

    1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置  1.命令行参数 所有的配置都可以在命令行上进行指定 ...

  3. SpringBoot——配置文件加载位置及外部配置加载顺序

    声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...

  4. Spring Boot 配置加载顺序详解

    使用 Spring Boot 会涉及到各种各样的配置,如开发.测试.线上就至少 3 套配置信息了.Spring Boot 可以轻松的帮助我们使用相同的代码就能使开发.测试.线上环境使用不同的配置. 在 ...

  5. Spring Boot配置加载顺序

    如果加载的配置有重复的,它们的加载顺序是这样的,数字越小的优先级越高,即优先级高的覆盖优先级低的配置. Devtools global settings properties on your home ...

  6. 【串线篇】spring boot外部配置加载顺序

    SpringBoot也可以从以下位置加载配置: 原则仍然是优先级从高到低:高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 java -j ...

  7. web.xml 配置 加载顺序

    web.xml 的加载顺序是:context-param -> listener -> filter -> servlet . 过滤器执行顺序是根据filter-mapping ,不 ...

  8. deepin 环境变量配置加载顺序

    加载顺序,默认是没有 .bash_profile 文件 1. non-login 方式 ~$ su deepin run /ect/bash.bashrc run .bashrc 2.login 方式 ...

  9. Springboot配置文件加载顺序

    使用Springboot开发的时候遇到了配置的问题,外部config里的配置文件本来没有配置https怎么启动还是https呢,原来开发中测试https在classpath路径的配置文件添加https ...

随机推荐

  1. Android实用代码七段(二)

    正文 一.获取应用程序下所有Activity public static ArrayList<String> getActivities(Context ctx) {       Arra ...

  2. (转)Android 仿订单出票效果 (附DEMO)

    之前我下载了BaseAnimation 开源库(BaseAnimation是基于开源的APP,致力于收集各种动画效果) BaseAnimation 转载的链接:http://blog.csdn.net ...

  3. [Java反射机制]用反射改进简单工厂模式设计

    如果做开发的工作,工厂设计模式大概都已经深入人心了,比较常见的例子就是在代码中实现数据库操作类,考虑到后期可能会有数据库类型变换或者迁移,一般都会对一个数据库的操作类抽象出来一个接口,然后用工厂去获取 ...

  4. Python中的SQLAlchemy

    在Python中,使用SQLAlchemy可以对数据库进行操作. SQLAlchemy是Python中的一个标准库. 要使用SQLAlchemy,首先要创建连接: url = mysql+pymysq ...

  5. rtmp发布录制视频

    本文描述了rtmp发布本地视频的流程 一.简要介绍 RTMP协议规定,播放一个流媒体有两个前提步骤:第一步,建立一个网络连接(NetConnection):第二步,建立一个网络流(NetStream) ...

  6. 2017-2018-1 1623 bug终结者 冲刺006

    bug终结者 冲刺006 by 20162328 蔡文琛 今日任务:音频素材添加 又是新的一天,小组项目有了很大的起色,已经可以在手机上试玩了. 添加背景音乐能使我们的游戏锦上添花. 音频资源需求 需 ...

  7. JAVA使用和操作properties文件

    java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properti ...

  8. 使用XIB实现嵌套自定义视图

    在进行iOS开发的过程中,对于一些复杂的界面,我们可以通过Interface Builder这个Xcode集成的可视化界面编辑工具在完成,这回节省大部分时间以及代码量.它的使用方法这里不做介绍了,这次 ...

  9. node创建第一个应用

    如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...

  10. jquery基础总结 -- 转载

    jquery的each里面return的使用 在使用jquery的each方法时, return false相当于break,是跳出each循环,return true相当于continue,是继续循 ...