欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談關(guān)于spring profile的誤解

 更新時間:2018年08月16日 10:05:26   作者:Mr_Qi  
這篇文章主要介紹了淺談關(guān)于spring profile的誤解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

背景

spring的profile大家都是用的溜的飛起~

那么profile的組合如何使用呢???

比如我們這樣使用

@Profile({"prod", "unit-test"})

分析

上述的profile大家應(yīng)該不會存有疑問 當profile為prod或者unit-test的時候才會生效。

但是如果我們使用非呢~如何確保在某些情況下不生效!

spring提供了常見的!來進行描述

因此如果想要在非生產(chǎn)環(huán)境生效只要簡單的寫成

@Profile({"!prod"})

那么如何在多個環(huán)境下不生效呢???

自作聰明的某些人【我】如下代碼

@Profile({"!prod", "!unit-test"})

那么實際情況是否如此呢???

我們看一下對應(yīng)的代碼

代碼

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);

從上述可以看到應(yīng)該是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

因此上述邏輯告訴我們其實應(yīng)該是或者的邏輯。因此

@Profile({"!prod", "!unit-test"})

!prod||!unit-test===>!(prod&&unit-test)  也就是說當prod和unit-test都生效的時候才不會注冊 其他調(diào)均都會注冊生效

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot導(dǎo)出Excel表格到指定路徑的代碼詳解

    SpringBoot導(dǎo)出Excel表格到指定路徑的代碼詳解

    Spring Boot導(dǎo)出Excel通常涉及到使用第三方庫如Apache POI或者XlsxWriter等,它們能幫助你在Spring應(yīng)用中生成并下載Excel文件,那么SpringBoot如何導(dǎo)出Excel表格到指定路徑,本文將給大家詳細的介紹一下
    2024-07-07
  • Java通過關(guān)閉Socket終止線程

    Java通過關(guān)閉Socket終止線程

    這篇文章主要為大家詳細介紹了Java通過關(guān)閉Socket終止線程的相關(guān)代碼
    2017-04-04
  • Java集合的組內(nèi)平均值的計算方法總結(jié)

    Java集合的組內(nèi)平均值的計算方法總結(jié)

    在Java中,經(jīng)常需要對集合進行各種操作,其中之一就是計算集合的組內(nèi)平均值,本文將介紹如何使用Java集合來計算組內(nèi)平均值,并提供一些示例代碼和實用技巧
    2024-08-08
  • 舉例分析Python中設(shè)計模式之外觀模式的運用

    舉例分析Python中設(shè)計模式之外觀模式的運用

    這篇文章主要介紹了Python中設(shè)計模式之外觀模式的運用,外觀模式主張以分多模塊進行代碼管理而減少耦合,需要的朋友可以參考下
    2016-03-03
  • Java ArrayDeque使用方法詳解

    Java ArrayDeque使用方法詳解

    這篇文章主要為大家詳細介紹了Java ArrayDeque的使用方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • springAop實現(xiàn)講解(看這篇夠了)

    springAop實現(xiàn)講解(看這篇夠了)

    AOP面向切面編程是一種編程范式,它通過將通用的橫切關(guān)注點(如日志、事務(wù)、權(quán)限控制等)與業(yè)務(wù)邏輯分離,使得代碼更加清晰、簡潔、易于維護,這篇文章主要介紹了springAop實現(xiàn)講解(看這篇夠了),需要的朋友可以參考下
    2024-02-02
  • Java單鏈表基本操作的實現(xiàn)

    Java單鏈表基本操作的實現(xiàn)

    鏈表是一種數(shù)據(jù)結(jié)構(gòu),和數(shù)組同級。接下來通過本文給大家介紹Java單鏈表基本操作的實現(xiàn),非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧
    2016-07-07
  • 淺談Maven 項目中依賴的搜索順序

    淺談Maven 項目中依賴的搜索順序

    這篇文章主要介紹了淺談Maven 項目中依賴的搜索順序,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 基于SpringBoot實現(xiàn)圖片上傳與顯示

    基于SpringBoot實現(xiàn)圖片上傳與顯示

    這篇文章主要為大家詳細介紹了基于SpringBoot實現(xiàn)圖片上傳與顯示,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 解析SpringBoot中@Autowire注解的實現(xiàn)原理

    解析SpringBoot中@Autowire注解的實現(xiàn)原理

    在開發(fā)Java項目時,依賴注入是一種常見的實現(xiàn)方式,SpringBoot框架通過@Autowired注解來實現(xiàn)依賴注入的功能,本文將介紹SpringBoot中 Autowired注解實現(xiàn)的原理
    2023-06-06

最新評論