当前位置:网站首页>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.
边栏推荐
- Sow of PMP
- This time, thoroughly understand bidirectional data binding 01
- The difference between SRAM and DRAM
- How the computer flushes the local DNS cache
- Learning notes of raspberry pie 4B - IO communication (SPI)
- Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
- Summary of basic knowledge of exception handling
- Esp-idf turns off serial port log output.
- Sort merge sort
- Yyds dry goods inventory Spring Festival "make" your own fireworks
猜你喜欢
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
pivot ROP Emporium
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
Buuctf, misc: n solutions
Morning flowers and evening flowers
Learning notes of raspberry pie 4B - IO communication (SPI)
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
IPhone development swift foundation 09 assets
The reason why the computer runs slowly and how to solve it
随机推荐
2022 electrician (elementary) examination questions and electrician (elementary) registration examination
webAssembly
Oil monkey plug-in
[golang] leetcode intermediate - alphabetic combination of island number and phone number
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
[template summary] - binary search tree BST - Basics
Mysql database - Advanced SQL statement (I)
Take you to master the formatter of visual studio code
2022 high altitude installation, maintenance and removal of examination question bank and high altitude installation, maintenance and removal of examination papers
The reason why the computer runs slowly and how to solve it
Firefox set up proxy server
pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
[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)
Covariance
2 spark environment setup local
Classification and extension of OC
Comparable interface and comparator interface
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
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?