溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何驗證Spring 中的Controller 是單例還是多例

發(fā)布時間:2020-11-06 16:27:30 來源:億速云 閱讀:421 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了如何驗證Spring 中的Controller 是單例還是多例,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

Spring管理的Controller,即加入@Controller 注入的類,默認是單例的,因此建議:

1、不要在Controller 中定義成員變量;(單例非線程安全,會導(dǎo)致屬性重復(fù)使用)

2、若必須要在Controller 中定義一個非靜態(tài)成員變量,則通過注解@Scope("prototype"),將其設(shè)置為多例模式。

二、驗證Controller 單例

驗證代碼:

package com.ausclouds.bdbsec.tjt;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author tjt
 * @time 2020-08-25
 * @desc 驗證Controller 單例
 */
@Controller
@ResponseBody
@RequestMapping("/tjt")
public class TestSingleController {

  private long money = 10;

  @GetMapping("/test1")
  public long testSingleOne(){
    money = ++money;
    System.out.println("/tjt/test1: the money I have: " + money);
    return money;
  }

  @GetMapping("test2")
  public long testSingleTwo(){
    money = ++money;
    System.out.println("/tjt/test2: the money I have: " + money);
    return money;
  }

}

首先,訪問http://localhost:8088/test1,得到的答案是11;

接著,再訪問http://localhost:8088/test2,得到的答案是 12;

不難看出:同一個變量,兩次訪問得到不同的結(jié)果,很明顯是線程不安全的。

驗證截圖:

如何驗證Spring 中的Controller 是單例還是多例

三、Controller 如何實現(xiàn)多例?

盡量不要在Controller 中定義成員變量,若必須要在Controller 中定義一個非靜態(tài)成員變量,則通過注解@Scope("prototype"),將其設(shè)置為多例模式;或者是在Controller 中使用ThreadLocal 變量。

驗證代碼:

package com.ausclouds.bdbsec.tjt;

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

/**
 * @author tjt
 * @time 2020-08-25
 * @desc 驗證Controller 單例
 */
@Controller
@ResponseBody
@Scope("prototype") // 將Controller 設(shè)置為多例模式
@RequestMapping("/tjt")
public class TestSingleController {

  private long money = 10;

  @GetMapping("/test1")
  public long testSingleOne(){
    money = ++money;
    System.out.println("/tjt/test1: after use @Scope the money I have: " + money);
    return money;
  }

  @GetMapping("test2")
  public long testSingleTwo(){
    money = ++money;
    System.out.println("/tjt/test2: after use @Scope the money I have: " + money);
    return money;
  }

}

在加上@Scope("prototype")后首先,訪問http://localhost:8088/test1,得到的答案是11;

接著,再訪問http://localhost:8088/test2,得到的答案也是 11;

不難看出:同一個變量,兩次訪問得到相同的結(jié)果。

驗證截圖:

如何驗證Spring 中的Controller 是單例還是多例

四、作用域

其實,spring bean 的作用域除了上面使用的prototype 外,還有singleton、request、session 和global session 四種;其中request、session 和global session 主要運用在Web 項目中。

  • singleton:單例模式,當(dāng)spring 創(chuàng)建applicationContext 容器的時候,spring會預(yù)初始化所有的該作用域?qū)嵗?,加上lazy-init 就可以避免預(yù)處理;
  • prototype:原型模式,每次通過getBean 獲取該bean 就會新產(chǎn)生一個實例,創(chuàng)建后spring 將不再對其管理;
  • request:每次請求都新產(chǎn)生一個實例,和prototype 不同就是創(chuàng)建后,接下來的管理,spring依然在監(jiān)聽;
  • session:每次會話,同上;
  • global session:全局的web 域,類似于servlet 中的application。

上述內(nèi)容就是如何驗證Spring 中的Controller 是單例還是多例,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI