springprofile的示例分析

這篇文章給大家分享的是有關(guān)spring profile的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實效"的一站式服務(wù),以成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、移動互聯(lián)產(chǎn)品、成都全網(wǎng)營銷服務(wù)為核心業(yè)務(wù)。十載網(wǎng)站制作的經(jīng)驗,使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標準網(wǎng)站,不但價格便宜而且實用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。

背景

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)均都會注冊生效

感謝各位的閱讀!關(guān)于“spring profile的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)頁名稱:springprofile的示例分析
當前地址:http://bm7419.com/article38/psshsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信小程序、做網(wǎng)站、定制開發(fā)網(wǎng)站營銷、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)