Spring 的Controller 是單例or多例

Spring 的Controller 是單例or多例

 

      你什麼也不肯放棄,又得到了什麼?

 

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

一、面試回答

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

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

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

二、驗證Controller 單例

驗證代碼:

 1 package com.ausclouds.bdbsec.tjt;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 /**
 9  * @author tjt
10  * @time 2020-08-25
11  * @desc 驗證Controller 單例
12  */
13 @Controller
14 @ResponseBody
15 @RequestMapping("/tjt")
16 public class TestSingleController {
17 
18     private long money = 10;
19 
20     @GetMapping("/test1")
21     public long testSingleOne(){
22         money = ++money;
23         System.out.println("/tjt/test1: the money I have: " + money);
24         return money;
25     }
26 
27     @GetMapping("test2")
28     public long testSingleTwo(){
29         money = ++money;
30         System.out.println("/tjt/test2: the money I have: " + money);
31         return money;
32     }
33 
34 }

View Code-拍一拍

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

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

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

驗證截圖:

 三、Controller 如何實現多例?

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

驗證代碼:

 1 package com.ausclouds.bdbsec.tjt;
 2 
 3 import org.springframework.context.annotation.Scope;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 /**
10  * @author tjt
11  * @time 2020-08-25
12  * @desc 驗證Controller 單例
13  */
14 @Controller
15 @ResponseBody
16 @Scope("prototype") // 將Controller 設置為多例模式
17 @RequestMapping("/tjt")
18 public class TestSingleController {
19 
20     private long money = 10;
21 
22     @GetMapping("/test1")
23     public long testSingleOne(){
24         money = ++money;
25         System.out.println("/tjt/test1: after use @Scope the money I have: " + money);
26         return money;
27     }
28 
29     @GetMapping("test2")
30     public long testSingleTwo(){
31         money = ++money;
32         System.out.println("/tjt/test2: after use @Scope the money I have: " + money);
33         return money;
34     }
35 
36 }

View Code-拍一拍

在加上@Scope(“prototype”)後首先,訪問 //localhost:8088/test1,得到的答案是11

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

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

驗證截圖:

四、作用域

其實,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。

 

 

你什麼也不肯放棄,又得到了什麼?