浅谈关于spring profile的误解
作者:Mr_Qi 发布时间:2021-07-25 05:48:43
标签:spring,profile
背景
spring的profile大家都是用的溜的飞起~
那么profile的组合如何使用呢???
比如我们这样使用
@Profile({"prod", "unit-test"})
分析
上述的profile大家应该不会存有疑问 当profile为prod或者unit-test的时候才会生效。
但是如果我们使用非呢~如何确保在某些情况下不生效!
spring提供了常见的!来进行描述
因此如果想要在非生产环境生效只要简单的写成
@Profile({"!prod"})
那么如何在多个环境下不生效呢???
自作聪明的某些人【我】如下代码
@Profile({"!prod", "!unit-test"})
那么实际情况是否如此呢???
我们看一下对应的代码
代码
profile是通过profileCondition来完成控制的
class ProfileCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
}
return true;
}
}
很明显可以看到了acceptsProfiles
/**
* Return whether one or more of the given profiles is active or, in the case of no
* explicit active profiles, whether one or more of the given profiles is included in
* the set of default profiles. If a profile begins with '!' the logic is inverted,
* i.e. the method will return true if the given profile is <em>not</em> active.
* For example, <pre class="code">env.acceptsProfiles("p1", "!p2")</pre> will
* return {@code true} if profile 'p1' is active or 'p2' is not active.
* @throws IllegalArgumentException if called with zero arguments
* or if any profile is {@code null}, empty or whitespace-only
* @see #getActiveProfiles
* @see #getDefaultProfiles
*/
boolean acceptsProfiles(String... profiles);
从上述可以看到应该是or的条件
当然代码如下
@Override
public boolean acceptsProfiles(String... profiles) {
Assert.notEmpty(profiles, "Must specify at least one profile");
for (String profile : profiles) {
if (StringUtils.hasLength(profile) && profile.charAt(0) == '!') {
if (!isProfileActive(profile.substring(1))) {
return true;
}
}
else if (isProfileActive(profile)) {
return true;
}
}
return false;
}
因此可以看到当是!条件的时候会判断如果当前未激活profile返回true 否则当前是正常条件的换当前profile如果激活则返回true 当上述条件都不满足才返回false
因此上述逻辑告诉我们其实应该是或者的逻辑。因此
@Profile({"!prod", "!unit-test"})
!prod||!unit-test===>!(prod&&unit-test) 也就是说当prod和unit-test都生效的时候才不会注册 其他调均都会注册生效
来源:https://my.oschina.net/qixiaobo025/blog/1927580


猜你喜欢
- 注册网建短信通账号链接:http://sms.webchinese.cn/设置短信签名注意不要乱写别的公司等,会被视为 * 设置短信密钥,
- 前言:上篇总结了下WebApi的接口测试工具的使用,这篇接着来看看WebAPI的另一个常见问题:跨域问题。本篇主要从实例的角度分享下CORS
- 本文实例为大家分享了java音乐播放器的具体代码,供大家参考,具体内容如下源码:package baidu;import java.awt.
- Java中的set是无序的,但是是不可重复的HashSet底层是哈希表,通过调用hashcode和equals方法实现去重当我们HashSe
- Android Studio是谷歌推出一个Android集成开发工具,基于IntelliJ IDEA。它类似于Eclipse ADT,And
- 本文实例为大家分享了android实现底部导航栏的具体代码,供大家参考,具体内容如下常见的底部导航栏动态效果实现步骤1.底部导航栏样式我们应
- 在使用springMVC框架构建web应用,客户端常会请求字符串、整型、json等格式的数据,通常使用@ResponseBody注解使 co
- 实现Runnable 接口比继承Thread 类的方式更好:(1)可以避免由于Java单继承带来的局限性;(2)可以实现业务执行逻辑和数据资
- 本文实例为大家分享了java生成验证码图片的具体代码,供大家参考,具体内容如下示例一:import org.apache.commons.c
- 当前的Winform分页控件中,当前导出的数据一般使用Excel来处理,Excel的文档可以用于后期的数据展示或者批量导入做准备,因此是比较
- 现在越来越多的软件都开始使用沉浸式状态栏了,下面总结一下沉浸式状态栏的两种使用方法注意!沉浸式状态栏只支持安卓4.4及以上的版本状态栏:4.
- 上一篇:C# 异步多线程入门到精通之Thread篇下一篇:异步多线程之入Task,待更新启动线程池线程ThreadPool 提供的 API
- 本文实例为大家分享了Java通讯录系统的具体代码,供大家参考,具体内容如下import java.util.Scanner;class Pe
- 新建一个txt的文本(代码中读取这个文本文档路径就行,命名随意)里面的内容一行代表一个,因为我是按行来遍历循环读取要屏蔽的关键字.然后用一个
- 今天给大家介绍下用Java swing开发一款音乐播放器, * 酷狗音乐播放器,完整源码地址在最下方,本文只列出部分源码,因为源码很多,全部贴
- Session是服务器端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个其独享的session对象,注意是默认情况下,一个
- SpringBoot使用过滤器、 * 和 * 一、SpringBoot使用过滤器Spring boot过滤器的使用(两种方式)使用sprin
- 一、建数据库和表1.数据库demo1放一张user表SET FOREIGN_KEY_CHECKS=0;-- ----------------
- 经典排序算法 - 冒泡排序Bubble sort原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,这样一趟过去后,最大或
- 以前我们说过在一些简单的例子中,比如为一个字段赋值或递增该字段,我们需要对线程进行同步,虽然lock可以满足我们的需要,但是一个竞争锁一定会