In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
How does Spring's Controller ensure the security of concurrency? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Controller is singleton by default, do not use non-static member variables, otherwise data logic confusion will occur. It is not thread-safe because of the singleton.
Let's have a simple verification:
Package com.xttblog.springbootdemo.controller
Import org.springframework.context.annotation.Scope
Import org.springframework.stereotype.Controller
Import org.springframework.web.bind.annotation.RequestMapping
/ * *
* @ author amateur grass, official account
, /
@ 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)
}
}
We first visited http://localhost:8080/testScope and got an answer of 1.
Then we visit http://localhost:8080/testScope2 and the answer is 2.
Gets a different value, which is not thread-safe.
Next, let's add more examples of @ Scope ("prototype") to controller.
Package com.xttblog.springbootdemo.controller
Import org.springframework.context.annotation.Scope
Import org.springframework.stereotype.Controller
Import org.springframework.web.bind.annotation.RequestMapping
/ * *
* @ author amateur grass, official account
, /
@ 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 still visit http://localhost:8080/testScope first, and the answer is 1.
Then we visit http://localhost:8080/testScope2 and the answer is still 1.
I believe it is not difficult to find out:
❝
Singletons are unsafe and can cause attributes to be reused.
❞solutions do not define member variables in Controller. In case you have to define a non-static member variable, set it to multiple-instance mode by annotating @ Scope ("prototype"). Supplementary instructions on using the ThreadLocal variable in Controller
There are five spring bean scopes:
Singleton: singleton mode. When spring creates an applicationContext container, spring will want to initialize all instances of the scope and add lazy-init to avoid preprocessing; prototype: prototype mode, each time the bean is obtained through getBean, a new instance will be generated, and spring will no longer manage it after creation
(the following is only used under the web project)
Request: everyone engaged in web should understand the domain of request, that is, a new instance is generated for each request. Unlike prototype, after creation, spring is still listening; session: every session, same as above; global session: global web domain, similar to application in servlet. This is the answer to the question about how Spring's Controller ensures concurrent security. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.