浅谈关于spring profile的误解

 更新时间:2018年08月16日 10:05:26   作者:Mr_Qi  
这篇文章主要介绍了浅谈关于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都生效的时候才不会注册 其他调均都会注册生效

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java编程泛型限定代码分享

    Java编程泛型限定代码分享

    这篇文章主要介绍了Java编程泛型限定的相关内容,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • 深入理解Struts2国际化信息机制

    深入理解Struts2国际化信息机制

    本篇文章主要介绍了深入理解Struts2国际化信息机制,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 基于String实现同步锁的方法步骤

    基于String实现同步锁的方法步骤

    这篇文章主要给大家介绍了关于基于String实现同步锁的方法步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用String具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • Java StampedLock实现原理与最佳实践记录

    Java StampedLock实现原理与最佳实践记录

    本文介绍了Java 8引入的StampedLock,这是一种多模式同步控制组件,通过“戳”(stamp)标识锁的状态,支持写锁、悲观读锁和乐观读三种模式,StampedLock在特定场景下能够大幅提升系统性能,特别是在读多写少的场景中,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • Spring中@Transactional用法详细介绍

    Spring中@Transactional用法详细介绍

    这篇文章主要介绍了Spring中@Transactional用法详细介绍的相关资料,需要的朋友可以参考下
    2017-02-02
  • 整理java读书笔记十五之java中的内部类

    整理java读书笔记十五之java中的内部类

    内部类是指在一个外部类的内部再定义一个类。类名不需要和文件夹相同。本文给大家分享java读书笔记十五之java中的内部类,对java读书笔记相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • SpringBoot+RabbitMQ实现消息可靠传输详解

    SpringBoot+RabbitMQ实现消息可靠传输详解

    消息的可靠传输是面试必问的问题之一,保证消息的可靠传输主要在生产端开启 comfirm 模式,RabbitMQ 开启持久化,消费端关闭自动 ack 模式。本文将详解SpringBoot整合RabbitMQ如何实现消息可靠传输,需要的可以参考一下
    2022-05-05
  • Flink JobGraph生成源码解析

    Flink JobGraph生成源码解析

    这篇文章主要为大家介绍了Flink JobGraph生成源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Java中Lombok工具库使用的技术指南

    Java中Lombok工具库使用的技术指南

    Lombok 是 Java 开发中常用的工具库,通过注解的方式大大简化了代码开发,本篇文章将从基础入门到高级用法,深入讲解 Lombok 的使用技巧和注意事项,快跟随小编一起来学习一下吧
    2025-04-04
  • springboot-jpa的实现操作

    springboot-jpa的实现操作

    这篇文章主要介绍了springboot-jpa的实现操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03

最新评论