一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 淺談關(guān)于spring profile的誤解

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

2021-05-25 13:16Mr_Qi Java教程

這篇文章主要介紹了淺談關(guān)于spring profile的誤解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧

背景

springprofile大家都是用的溜的飛起~

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

比如我們這樣使用

?
1
@profile({"prod", "unit-test"})

分析

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

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

spring提供了常見的!來(lái)進(jìn)行描述

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

?
1
@profile({"!prod"})

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

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

?
1
@profile({"!prod", "!unit-test"})

那么實(shí)際情況是否如此呢???

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

代碼

profile是通過profilecondition來(lái)完成控制的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 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的條件

當(dāng)然代碼如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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;
}

因此可以看到當(dāng)是!條件的時(shí)候會(huì)判斷如果當(dāng)前未激活profile返回true 否則當(dāng)前是正常條件的換當(dāng)前profile如果激活則返回true 當(dāng)上述條件都不滿足才返回false

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

?
1
@profile({"!prod", "!unit-test"})

!prod||!unit-test===>!(prod&&unit-test)  也就是說(shuō)當(dāng)prod和unit-test都生效的時(shí)候才不會(huì)注冊(cè) 其他調(diào)均都會(huì)注冊(cè)生效

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://my.oschina.net/qixiaobo025/blog/1927580

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 狠狠色狠狠色综合日日小蛇 | 国产三级精品三级男人的天堂 | 欧美午夜视频一区二区 | 我将她侵犯1~6樱花动漫在线看 | bestialitysex杂交| 国产成人精品系列在线观看 | 国产乱插 | 国内自拍成人网在线视频 | 亚洲精品综合一二三区在线 | 美女尿口羞羞视频 | 亚洲精品久久麻豆蜜桃 | 亚洲欧美在线观看首页 | 四虎影院在线免费观看视频 | 国产精品久久免费观看 | 成年人免费看的视频 | 5x社区在线观看直接进入 | 农村妇女野外牲交一级毛片 | 污书屋| 日本黄大片影院一区二区 | 久久亚洲精品AV成人无码 | 国产成人盗摄精品 | 成人动漫在线免费看 | 日韩在线视精品在亚洲 | 国产精品久久久久这里只有精品 | 成人毛片高清视频观看 | 欧美一级专区免费大片俄罗斯 | 欧美日韩高清完整版在线观看免费 | 精品欧美男同同性videos | 国产1区2区三区不卡 | 521色香蕉网在线观看免费 | 日韩精品一区二区三区老鸭窝 | 久久免费国产视频 | 亚洲 欧美 中文字幕 在线 | 蜜臀91| bbc japanese黑人强行 | 亚洲99久久无色码中文字幕 | jiujiure精品 | 亚洲精品久久啪啪网站成年 | 动漫jk美女被爆羞羞漫画 | 国产成人精品免费视频大全五级 | 久久re这里精品在线视频7 |