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 }
首先,访问 http://localhost:8088/test1
,得到的答案是11
;
接着,再访问 http://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 }
在加上@Scope("prototype")后首先,访问 http://localhost:8088/test1
,得到的答案是11
;
接着,再访问 http://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。
你什么也不肯放弃,又得到了什么?