当前位置:网站首页>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.
边栏推荐
- How to switch between dual graphics cards of notebook computer
- Subset enumeration method
- JS closure knowledge points essence
- The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended
- Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
- 320. Energy Necklace (ring, interval DP)
- [Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
- Esp-idf turns off serial port log output.
- Conditional statements of shell programming
- [automation operation and maintenance novice village] flask-2 certification
猜你喜欢

2022 free examination questions for safety management personnel of hazardous chemical business units and reexamination examination for safety management personnel of hazardous chemical business units

Can you draw with turtle?
![[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)](/img/1d/044e81258db86cf34eddd3b8f5cf90.jpg)
[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)

How the computer flushes the local DNS cache

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!

Blue Bridge Cup -- guess age

2 spark environment setup local

Cesium terrain clipping draw polygon clipping

How to solve the problem of computer networking but showing no Internet connection

Firefox set up proxy server
随机推荐
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
This time, thoroughly understand bidirectional data binding 01
Es6~es12 knowledge sorting and summary
How to obtain opensea data through opensea JS
Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
Hcip day 14 notes
Opengauss database log management guide
Ppt image processing
2022 high altitude installation, maintenance and removal of examination question bank and high altitude installation, maintenance and removal of examination papers
Yyds dry goods inventory hands-on teach you to create a jigsaw puzzle using the canvasapi
Yyds dry goods inventory Spring Festival "make" your own fireworks
33 restrict the input of qlineedit control (verifier)
Classification and extension of OC
Flutter internationalized Intl
Hcip 13th day notes
Data consistency between redis and database
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
SDMU OJ#P19. Stock trading
The latest analysis of R1 quick opening pressure vessel operation in 2022 and the examination question bank of R1 quick opening pressure vessel operation
Buuctf, web:[geek challenge 2019] buyflag