<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 是單例還是多例?多少人答對?

          共 2217字,需瀏覽 5分鐘

           ·

          2020-09-13 08:03

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


          答案

          controller默認是單例的,不要使用非靜態(tài)的成員變量,否則會發(fā)生數(shù)據(jù)邏輯混亂。正因為單例所以不是線程安全的。

          我們下面來簡單的驗證下:

          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;然后我們再訪問 http://localhost:8080/testScope2,得到的答案是 2。

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

          接下來我們再來給controller增加作用多例 @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;然后我們再訪問 http://localhost:8080/testScope2,得到的答案還是 1

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

          單例是不安全的,會導致屬性重復使用。

          解決方案

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

          補充說明

          spring bean作用域有以下5個:

          1.singleton:單例模式,當spring創(chuàng)建applicationContext容器的時候,spring會欲初始化所有的該作用域實例,加上lazy-init就可以避免預處理;
          2.prototype:原型模式,每次通過getBean獲取該bean就會新產(chǎn)生一個實例,創(chuàng)建后spring將不再對其管理;
          (下面是在web項目下才用到的)
          3.request:搞web的大家都應該明白request的域了吧,就是每次請求都新產(chǎn)生一個實例,和prototype不同就是創(chuàng)建后,接下來的管理,spring依然在監(jiān)聽;
          4.session:每次會話,同上;
          5.global session:全局的web域,類似于servlet中的application。


          最后免費給大家分享50個Java項目實戰(zhàn)資料,涵蓋入門、進階各個階段學習內(nèi)容,可以說非常全面了。大部分視頻還附帶源碼,學起來還不費勁!


          附上截圖。(下面有下載方式)。


          項目領取方式:

          掃描下方公眾號回復:50,

          可獲取下載鏈接

          ???

          ?長按上方二維碼?2 秒
          回復「50」即可獲取資料


          點贊是最大的支持?

          瀏覽 60
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  免费黄色视频网站 | 无码潮喷 | 波多野结衣不打码视频 | 日本乱婬妺妺躁爽A片 | 99在线视频精品 |