浅谈关于spring profile的误解

作者:Mr_Qi 时间:2021-07-25 05:48:43 

背景

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

标签:spring,profile
0
投稿

猜你喜欢

  • Java实现添加条形码到PDF表格的方法详解

    2023-04-26 12:37:25
  • C# WebApi 接口返回值不困惑:返回值类型详解

    2022-05-06 13:16:35
  • java使double保留两位小数的多方法 java保留两位小数

    2023-06-17 14:01:30
  • 浅谈android Fragment横竖屏翻转对重新加载的要求

    2023-07-27 21:55:28
  • Java编程探索之泛型擦除实例解析

    2022-08-30 02:13:35
  • Spring Cloud负载均衡及远程调用实现详解

    2021-10-16 01:11:27
  • Android实现excel/pdf/word/odt/图片相互转换

    2021-12-05 23:08:16
  • SpringBoot事件发布和监听详解

    2022-04-18 02:39:59
  • Android虚拟机与类加载机制详情

    2022-12-16 20:09:03
  • C# 修改文件的创建、修改和访问时间的示例

    2023-06-09 23:18:50
  • 如何使用Java调用Linux系统命令

    2021-12-24 20:45:31
  • SpringBoot在一定时间内限制接口请求次数的实现示例

    2021-10-12 04:28:52
  • C#值类型、引用类型中的Equals和==的区别浅析

    2022-09-05 23:04:24
  • Android拼接实现动态对象方法详解

    2021-12-23 10:09:46
  • Android多线程学习实例详解

    2022-02-17 19:00:56
  • Spring Security认证提供程序示例详解

    2022-11-07 18:06:40
  • Android中手机震动的设置(Vibrator)的步骤简要说明

    2021-08-18 08:50:29
  • Java源码解析HashMap的keySet()方法

    2023-11-11 06:33:05
  • 一篇文章带你轻松了解C# Lock关键字

    2023-08-15 20:48:32
  • 浅谈Java 类中各成分加载顺序和内存中的存放位置

    2022-12-23 17:24:23
  • asp之家 软件编程 m.aspxhome.com