国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

淺談Spring 的Controller 是單例or多例

瀏覽:94日期:2023-08-18 10:43:40

背景:今天寫(xiě)代碼遇到一個(gè)Controller 中的線程安全問(wèn)題,那么Spring 的Controller 是單例還是多例的呢?若為單例又如何保證并發(fā)安全呢?

一、面試回答

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

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

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

二、驗(yàn)證Controller 單例

驗(yàn)證代碼:

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 驗(yàn)證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; }}

首先,訪問(wèn)http://localhost:8088/test1,得到的答案是11;

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

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

驗(yàn)證截圖:

淺談Spring 的Controller 是單例or多例

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

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

驗(yàn)證代碼:

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 驗(yàn)證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')后首先,訪問(wèn)http://localhost:8088/test1,得到的答案是11;

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

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

驗(yàn)證截圖:

淺談Spring 的Controller 是單例or多例

四、作用域

其實(shí),spring bean 的作用域除了上面使用的prototype 外,還有singleton、request、session 和global session 四種;其中request、session 和global session 主要運(yùn)用在Web 項(xiàng)目中。

singleton:?jiǎn)卫J剑?dāng)spring 創(chuàng)建applicationContext 容器的時(shí)候,spring會(huì)預(yù)初始化所有的該作用域?qū)嵗由蟣azy-init 就可以避免預(yù)處理; prototype:原型模式,每次通過(guò)getBean 獲取該bean 就會(huì)新產(chǎn)生一個(gè)實(shí)例,創(chuàng)建后spring 將不再對(duì)其管理; request:每次請(qǐng)求都新產(chǎn)生一個(gè)實(shí)例,和prototype 不同就是創(chuàng)建后,接下來(lái)的管理,spring依然在監(jiān)聽(tīng); session:每次會(huì)話,同上; global session:全局的web 域,類似于servlet 中的application。

到此這篇關(guān)于淺談Spring 的Controller 是單例or多例的文章就介紹到這了,更多相關(guān)Spring Controller 單例or多例內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 夹江县| 老河口市| 湄潭县| 罗平县| 西昌市| 米易县| 武乡县| 龙游县| 防城港市| 上林县| 汉沽区| 鄂托克旗| 文成县| 许昌市| 延川县| 肥城市| 焦作市| 乌兰浩特市| 普洱| 铜川市| 婺源县| 拜泉县| 安龙县| 梁山县| 元谋县| 个旧市| 礼泉县| 博野县| 辽阳县| 龙南县| 科尔| 调兵山市| 崇州市| 景洪市| 民乐县| 鄂托克前旗| 黔西县| 修水县| 延吉市| 锡林浩特市| 闽清县|