当前位置:网站首页>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.
边栏推荐
- . Net ADO splicing SQL statement with parameters
- Recursion and recursion
- 2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
- [sg function] 2021 Niuke winter vacation training camp 6 h. winter messenger 2
- Leetcode: a single element in an ordered array
- Blue Bridge Cup -- Mason prime
- How to restore the factory settings of HP computer
- Data consistency between redis and database
- 6.0 kernel driver character driver
- Druids connect to mysql8.0.11
猜你喜欢

Covariance

Buuctf, misc: n solutions

The difference between SRAM and DRAM

Take you to master the formatter of visual studio code

2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
![[SRS] build a specified version of SRS](/img/01/0d2d762e01b304220b8924d20277e3.jpg)
[SRS] build a specified version of SRS

Leetcode: a single element in an ordered array

webAssembly

Some 5000+ likes, the development notes of a director of cosmic factory, leaked
Creation of the template of the password management software keepassdx
随机推荐
[sg function] lightoj Partitioning Game
On my first day at work, this API timeout optimization put me down!
[template summary] - binary search tree BST - Basics
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
Redis single thread and multi thread
2022 safety officer-a certificate registration examination and summary of safety officer-a certificate examination
Team collaborative combat penetration tool CS artifact cobalt strike
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
Learning notes of raspberry pie 4B - IO communication (SPI)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Es6~es12 knowledge sorting and summary
33 restrict the input of qlineedit control (verifier)
Oil monkey plug-in
Why should enterprises do more application activities?
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
[sg function]split game (2020 Jiangxi university student programming competition)
IDENTITY
Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
What indicators should be paid attention to in current limit monitoring?
Blue Bridge Cup -- Mason prime