1. 程式人生 > 程式設計 >淺談Spring 的Controller 是單例or多例

淺談Spring 的Controller 是單例or多例

背景:今天寫程式碼遇到一個Controller 中的執行緒安全問題,那麼Spring 的Controller 是單例還是多例的呢?若為單例又如何保證併發安全呢?

一、面試回答

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

1、不要在Controller 中定義成員變數;(單例非執行緒安全,會導致屬性重複使用)

2、若必須要在Controller 中定義一個非靜態成員變數,則通過註解@Scope("prototype"),將其設定為多例模式。

二、驗證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

不難看出:同一個變數,兩次訪問得到不同的結果,很明顯是執行緒不安全的。

驗證截圖:

淺談Spring 的Controller 是單例or多例

三、Controller 如何實現多例?

儘量不要在Controller 中定義成員變數,若必須要在Controller 中定義一個非靜態成員變數,則通過註解@Scope("prototype"),將其設定為多例模式;或者是在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 設定為多例模式
@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

不難看出:同一個變數,兩次訪問得到相同的結果。

驗證截圖:

淺談Spring 的Controller 是單例or多例

四、作用域

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

  • singleton:單例模式,當spring 建立applicationContext 容器的時候,spring會預初始化所有的該作用域例項,加上lazy-init 就可以避免預處理;
  • prototype:原型模式,每次通過getBean 獲取該bean 就會新產生一個例項,建立後spring 將不再對其管理;
  • request:每次請求都新產生一個例項,和prototype 不同就是建立後,接下來的管理,spring依然在監聽;
  • session:每次會話,同上;
  • global session:全域性的web 域,類似於servlet 中的application。

到此這篇關於淺談Spring 的Controller 是單例or多例的文章就介紹到這了,更多相關Spring Controller 單例or多例內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!