spring.profiles.include 跟 spring.profiles.active 的区别
spring.profiles.include 跟 spring.profiles.active 的区别
背景
我们经常看到springboot的 spring.profiles.active
,有时也会看到 spring.profiles.include
这个配置,通常还是在spring.profiles.active
存在的情况下额外有include这个配置,那么区别是什么呢?
单用 spring.profiles.include
跟 spring.profiles.active
无区别,一起用的话,最终的profile的顺序是先include的,再active的,也就是active里的同名key会覆盖include的(后面的优先级更高),如
spring.profiles.active=A,B
spring.profiles.include=C,D
最终是这样的顺序 C,D,A,B
官方文档可以参考附录,其实官网虽然权威,但读起来干巴巴的,还是得结合自己动手做个实验加强对官网的描述的理解。
实验
application.properties
spring.profiles.active=dev
application-dev.properties
common.key=dev only.dev=dev
application-prod.properties
common.key=prod only.prod=prod
java code
@GetMapping("/getValue") public String getValue() {String[] activeProfiles = environment.getActiveProfiles();String[] defaultProfiles = environment.getDefaultProfiles();System.out.println("activeProfiles:" + Arrays.toString(activeProfiles));System.out.println("defaultProfiles:" + Arrays.toString(defaultProfiles));System.out.println("common.key:" + environment.getProperty("common.key"));System.out.println("only.dev:" + environment.getProperty("only.dev"));System.out.println("only.prod:" + environment.getProperty("only.prod"));return "success"; }
打印结果
activeProfiles:[dev]
defaultProfiles:[default]
common.key:dev
only.dev:dev
only.prod:null
上述 application.properties 里的内容改成(不是加上!)
spring.profiles.include=dev
则打印
activeProfiles:[dev]
defaultProfiles:[default]
common.key:dev
only.dev:dev
only.prod:null
跟 spring.profiles.active
一样的结果
如果 application.properties 的内容改成
spring.profiles.active=dev,prod
则打印
activeProfiles:[dev, prod]
defaultProfiles:[default]
common.key:prod
only.dev:dev
only.prod:prod
如果调换一下profile的顺序,改成
spring.profiles.active=prod,dev
则打印
activeProfiles:[prod, dev]
defaultProfiles:[default]
common.key:dev
only.dev:dev
only.prod:prod
可以发现 common.key
的值随着顺序的调整变了,后面的profile覆盖前面的
结论: spring.profiles.active=profile1,profile2
这种写法,相同key时后面的profile里的配置覆盖前面的(不相同的merge在一起)
spring.profiles.include=profile1,profile2也是后面的覆盖前面的?,测试一下:
# 如果 application.properties 的内容改成 spring.profiles.include=dev,prod 时所打印的结果如下
activeProfiles:[dev, prod]
defaultProfiles:[default]
common.key:prod
only.dev:dev
only.prod:prod# 如果 application.properties 的内容改成 spring.profiles.include=prod,dev 时所打印的结果如下
activeProfiles:[prod, dev]
defaultProfiles:[default]
common.key:dev
only.dev:dev
only.prod:prod
可以发现 common.key
的值也是随着profile的顺序会受到影响
**结论:**spring.profiles.include 跟 spring.profiles.active 的配置一样,配多个profile的时候后面的profile里的key值t’h会覆盖前面的(如果key相同)
下面开始研究两种配置(active和include)同时存在的时候的行为,如果 application.properties 的内容改成
spring.profiles.active=dev
spring.profiles.include=prod
打印的日志
activeProfiles:[prod, dev]
defaultProfiles:[default]
common.key:dev
only.dev:dev
only.prod:prod
如果调换一下位置,application.properties 的内容改成
spring.profiles.active=prod
spring.profiles.include=dev
则测试结果是
activeProfiles:[dev, prod]
defaultProfiles:[default]
common.key:prod
only.dev:dev
only.prod:prod
结论:先将 include 里的profile排在前面,然后再排active的
如果active和include配置里头都有多个值的情况,即 application.properties 的内容改成如下
spring.profiles.active=dev,profileInActive
spring.profiles.include=prod,profileInInclude
则启动的时候打印的日志是(日志省略了不必要的信息)
The following profiles are active: prod,profileInInclude,dev,profileInActive
则可以进一步得到验证,最终profile的结果会是先把include的拿出来,然后再追加active的。另外profile不存在也不会报错,上述profileInInclude和profileInActive都是不存在的)
附录
上面是自己动手做实验,下面是官方的文档对于 spring.profiles.include
的解释
4.3.1. Adding Active Profiles
The spring.profiles.active property follows the same ordering rules as other properties: The highest PropertySource wins. This means that you can specify active profiles in application.properties and then replace them by using the command line switch.Sometimes, it is useful to have profil-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles. The SpringApplication entry point also has a Java API for setting additional profiles (that is, on top of those activated by the spring.profiles.active property). See the setAdditionalProfiles() method in SpringApplication.For example, when an application with the following properties is run by using the switch, --spring.profiles.active=prod, the proddb and prodmq profiles are also activated:---
my.property: fromyamlfile
---
spring.profiles: prod
spring.profiles.include:- proddb- prodmq
https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/ 搜 spring.profiles.include
读书笔记
- 里面提到
Sometimes, it is useful to have profil-specific properties that add to the active profiles rather than replace them
, 其中有个错误的单词,应该是 profile-specific- 首先 xxx-specific的意思是,比如 “运动量的要求是个人体质-specific的”,意思是根据个人体质不同会有不同,简单说就是因人而异。所以xxx-specific就是根据xxx的不同会有不同
- 这句话的意思,我理解就是有时候我们有些properties(其实就是key,比如上面的common.key),在include里的profile里的可以是增加进active里配置的profile的,而不是替换(意思应该就是include的profile是放在active的profile的前面的,所以不会替换掉active里的profile的key
The spring.profiles.include property can be used to unconditionally add active profiles
,这句话的unconditionally
比较难理解,评论区走起,它是什么意思?
spring.profiles.include 跟 spring.profiles.active 的区别相关推荐
- springboot启动spring.profiles.active和spring.profiles.include影响的区别
application.properties文件内容 spring.profiles.active=test4 pring.profiles.include=test3,test5 version=1 ...
- spring.profiles.active和spring.profiles.include的使用及区别说明
转自: spring.profiles.active和spring.profiles.include的使用及区别说明 下文笔者讲述spring.profiles.active和spring.profi ...
- Spring Boot 进阶,Spring Boot面试题高频出处
上一篇带领大家初步了解了如何使用 Spring Boot 搭建框架,通过 Spring Boot 和传统的 SpringMVC 架构的对比,我们清晰地发现 Spring Boot 的好处,它使我们的代 ...
- 自己动手,使用Spring Initializr从零开始搭建Spring Cloud项目
新建Project 这里使用的开发工具是IDEA,JDK版本1.8. 打开IDEA开发工具,File -> New -> Project 然后一步步往下设置,然后到这一步,选择Spring ...
- 使用 Spring Boot 快速构建 Spring 框架应用
https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/index.html Spring 框架对于很多 Java 开发人员来说都不陌生 ...
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...
- Spring Boot系列二 Spring @Async异步线程池用法总结
转载 自 https://blog.csdn.net/hry2015/article/details/67640534 1. TaskExecutor Spring异步线程池的接口类,其实质是java ...
- Spring Cloud Config 和Spring Cloud Bus实现配置中心
2019独角兽企业重金招聘Python工程师标准>>> Spring Cloud是很多组件的集合,Spring将常用的技术框架进行包装和整合,如mybatis zookeeper r ...
- 构建Spring Boot RESTful服务+ Spring Boot执行器
总览 什么是REST? REST(代表状态转移)是Web构建的体系结构样式,已成为用于Web应用程序的标准软件设计模式 . 代表性国家转移一词最早由REST的发起人,HTTP规范的主要作者之一Roy ...
最新文章
- python中定义类
- ctimespan 获取毫秒_VC++中通过CTime类获取日期差
- C++ 简单的语音合成(TTS,即文字转语音)类
- 22 SD配置-主数据-定义付款条款
- anaconda windows theano keras 完全安装教程,没有gpu忽略那段话也成功了
- 2021年中国一次性防护服市场趋势报告、技术动态创新及2027年市场预测
- 互联网年终福利大盘点,告诉你过年的正确姿势!
- OSI七层模型及TCP/IP五层模型
- 最新正版nero7序列号注册码
- ubuntn 16.04.2下caffee的安装教程
- Mac OS系统下kernel_task占用大量CPU资源导致系统卡顿
- 【科普向】5G核心网架构和关键技术
- 新手建网站的步骤及注意事项
- STL ++iter与iter++区别
- 玩转 MATLAB 附加功能/硬件支持包安装
- redis的incr+expire的坑
- 关于网线,你了解多少?
- a java rnvironme_分析113个前缀在各大考纲词汇中的作用(一)
- HTML5常用标签及属性
- html div 区域,div全称division,意为“区分”。div标签被称为区隔标签,表示一块可显示 HTML 的区域。DIV的主要属性有( )...