<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring 的 Controller 是單例還是多例?怎么保證并發(fā)的安全?

          共 2392字,需瀏覽 5分鐘

           ·

          2020-08-24 09:10

          點(diǎn)擊上方“碼農(nóng)突圍”,馬上關(guān)注

          這里是碼農(nóng)充電第一站,回復(fù)“666”,獲取一份專屬大禮包

          真愛,請(qǐng)?jiān)O(shè)置“星標(biāo)”或點(diǎn)個(gè)“在看”

          來源 | http://blog.csdn.net/riemann_/article/details/97698560

          • 答案
          • 得到的不同的值,這是線程不安全的
          • 解決方案
          • 補(bǔ)充說明

          答案

          controller默認(rèn)是單例的,不要使用非靜態(tài)的成員變量,否則會(huì)發(fā)生數(shù)據(jù)邏輯混亂。正因?yàn)閱卫圆皇蔷€程安全的。

          我們下面來簡(jiǎn)單的驗(yàn)證下:

          package?com.riemann.springbootdemo.controller;

          import?org.springframework.context.annotation.Scope;
          import?org.springframework.stereotype.Controller;
          import?org.springframework.web.bind.annotation.RequestMapping;

          /**
          ?*?@author?riemann
          ?*?@date?2019/07/29?22:56
          ?*/

          @Controller
          public?class?ScopeTestController?{

          ????private?int?num?=?0;

          ????@RequestMapping("/testScope")
          ????public?void?testScope()?{
          ????????System.out.println(++num);
          ????}

          ????@RequestMapping("/testScope2")
          ????public?void?testScope2()?{
          ????????System.out.println(++num);
          ????}

          }

          我們首先訪問 http://localhost:8080/testScope,得到的答案是1;

          然后我們?cè)僭L問 http://localhost:8080/testScope2,得到的答案是 2。

          得到的不同的值,這是線程不安全的

          接下來我們?cè)賮斫ocontroller增加作用多例 @Scope("prototype")

          package?com.riemann.springbootdemo.controller;

          import?org.springframework.context.annotation.Scope;
          import?org.springframework.stereotype.Controller;
          import?org.springframework.web.bind.annotation.RequestMapping;

          /**
          ?*?@author?riemann
          ?*?@date?2019/07/29?22:56
          ?*/

          @Controller
          @Scope("prototype")
          public?class?ScopeTestController?{

          ????private?int?num?=?0;

          ????@RequestMapping("/testScope")
          ????public?void?testScope()?{
          ????????System.out.println(++num);
          ????}

          ????@RequestMapping("/testScope2")
          ????public?void?testScope2()?{
          ????????System.out.println(++num);
          ????}

          }

          我們依舊首先訪問 http://localhost:8080/testScope,得到的答案是1;

          然后我們?cè)僭L問 http://localhost:8080/testScope2,得到的答案還是 1。

          相信大家不難發(fā)現(xiàn) :

          單例是不安全的,會(huì)導(dǎo)致屬性重復(fù)使用。

          解決方案

          • 不要在controller中定義成員變量。
          • 萬一必須要定義一個(gè)非靜態(tài)成員變量時(shí)候,則通過注解@Scope(“prototype”),將其設(shè)置為多例模式。
          • 在Controller中使用ThreadLocal變量

          補(bǔ)充說明

          spring bean作用域有以下5個(gè):

          • singleton: 單例模式,當(dāng)spring創(chuàng)建applicationContext容器的時(shí)候,spring會(huì)欲初始化所有的該作用域?qū)嵗由蟣azy-init就可以避免預(yù)處理;
          • prototype:原型模式,每次通過getBean獲取該bean就會(huì)新產(chǎn)生一個(gè)實(shí)例,創(chuàng)建后spring將不再對(duì)其管理;

          (下面是在web項(xiàng)目下才用到的)

          • request:搞web的大家都應(yīng)該明白request的域了吧,就是每次請(qǐng)求都新產(chǎn)生一個(gè)實(shí)例,和prototype不同就是創(chuàng)建后,接下來的管理,spring依然在監(jiān)聽;
          • session: 每次會(huì)話,同上;
          • global session: 全局的web域,類似于servlet中的application。

          END



          最近熱文:

          牛逼了,日本神秘男子用 AI 黑科技破解馬賽克,震驚業(yè)界大佬!

          牛逼!9種不同的方法幫助你提高國(guó)內(nèi)訪問Github的速度到2MB/s以上...

          Spring Boot 微信點(diǎn)餐開源系統(tǒng)!

          為什么建議大家使用 Linux 開發(fā)?爽(外加七個(gè)感嘆號(hào))


          最近整理了一份大廠算法刷題指南,包括一些刷題技巧,在知乎上已經(jīng)有上萬贊。同時(shí)還整理了一份6000頁面試筆記。關(guān)注下面公眾號(hào),在公眾號(hào)內(nèi)回復(fù)「刷題」,即可免費(fèi)獲取!回復(fù)「加群」,可以邀請(qǐng)你加入讀者群!



          明天見(??ω??)??

          瀏覽 56
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费黄色小视频网站 | caopen在线视频 | 国产亚洲中文字幕在线观看 | 久久久久91精品成人片 | 女人天堂中文字幕 |