当前位置:网站首页>Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
2022-07-03 22:42:00 【╱℡&▓】
answer :
controller The default is singleton , Don't use non static member variables , Otherwise, there will be data logic confusion . Because of the singleton, it's not thread safe .
Let's do a simple verification :
package com.riemann.springbootdemo.controller;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author riemann
* @date 2019/07/29 22:56
*/
@Controller
public class ScopeTestController {
private int num = 0;
@RequestMapping("/testScope")
public void testScope() {
System.out.println(++num);
}
@RequestMapping("/testScope2")
public void testScope2() {
System.out.println(++num);
}
} Let's first visit http://localhost:8080/testScope, The answer is 1;
Then we will visit http://localhost:8080/testScope2, The answer is 2.
Different values you get , This is thread unsafe .
Next we'll give controller There are many cases of increasing effect @Scope("prototype")
package com.riemann.springbootdemo.controller;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author riemann
* @date 2019/07/29 22:56
*/
@Controller
@Scope("prototype")
public class ScopeTestController {
private int num = 0;
@RequestMapping("/testScope")
public void testScope() {
System.out.println(++num);
}
@RequestMapping("/testScope2")
public void testScope2() {
System.out.println(++num);
}
} We are still the first to visit http://localhost:8080/testScope, The answer is 1;
Then we will visit http://localhost:8080/testScope2, The answer is still 1.
I believe it's not hard to find out :
Singleton is not safe , Will cause the attribute to be reused .
Solution
1、 Not in controller Define member variables in .2、 In case you have to define a non static member variable , By way of annotation @Scope(“prototype”), Set it to multi instance mode .3、 stay Controller Use in ThreadLocal Variable
Additional explanation
spring bean The scope is as follows 5 individual :
singleton: The singleton pattern , When spring establish applicationContext Container time ,spring Will want to initialize all instances of the scope , add lazy-init You can avoid preprocessing ;
prototype: Archetypal model , Each time through getBean Get the bean It will create a new instance , After creation spring It will no longer be managed ;
( The following is the web Only under the project )
request: To make web Everyone should understand request It's the domain of , That is, each request generates a new instance , and prototype The difference is that after creation , The next management ,spring Still monitoring ;
session: Every conversation , ditto ;
global session: Overall web Domain , Be similar to servlet Medium application.
边栏推荐
- Hcip day 16 notes
- Hcip day 12 notes
- Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
- Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
- [sg function] lightoj Partitioning Game
- C3p0 connection MySQL 8.0.11 configuration problem
- IDENTITY
- Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
- Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
- The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
猜你喜欢

Covariance

Blue Bridge Cup Guoxin Changtian MCU -- program download (III)

Harbor integrated LDAP authentication

Can you draw with turtle?

Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)

Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"

Format cluster and start cluster

Teach you how to run two or more MySQL databases at the same time in one system

Data consistency between redis and database

Hcip day 15 notes
随机推荐
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Shell script three swordsman awk
油猴插件
Kali2021.4a build PWN environment
Redis single thread and multi thread
Data consistency between redis and database
webAssembly
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
Hcip day 12 notes
[issue 16] golang's one-year experience in developing Purdue Technology
Awk getting started to proficient series - awk quick start
User login function: simple but difficult
Simple solution of m3u8 file format
Leetcode: a single element in an ordered array
How to solve win10 black screen with only mouse arrow
33 restrict the input of qlineedit control (verifier)
Summary of fluent systemchrome