In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Spring MVC Controller singleton trap is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Spring MVC Controller is singleton by default
The following are the test steps, code and results.
1. If it is a singleton type class, then the class variables in the Controller class should be shared, if not shared, it means that the Controller class is not a singleton. Here is the test code:
Import javax.servlet.http.HttpServletRequest
Import javax.servlet.http.HttpServletResponse
Import org.springframework.stereotype.Controller
Import org.springframework.web.bind.annotation.RequestMapping
Import org.springframework.web.bind.annotation.ResponseBody
@ Controller
Public class ExampleAction {
Private int singletonInt=1
@ RequestMapping (value = "/ test")
@ ResponseBody
Public String singleton (HttpServletRequest request
HttpServletResponse response) throws Exception {
String data=request.getParameter ("data")
If (data length () > 0) {
Try {
Int paramInt= Integer.parseInt (data)
SingletonInt = singletonInt + paramInt
}
Catch (Exception ex) {
SingletonInt+=10
}
} else {
SingletonInt+=1000
}
Return String.valueOf (singletonInt)
}
}
Three requests respectively: http://localhost:8080/example/test.do?data=15
The return result is as follows.
First time: singletonInt=15
The second time: singletonInt=30
The third time: singletonInt=45
From the above results, we can see that the state of singletonInt is shared, so Controller is singleton.
There are two reasons for the single case:
1. For performance.
2. Multiple cases are not needed.
1. Don't talk nonsense about this. You don't have to new every time in a single case. Of course it's fast.
2. No need for an instance will confuse many people, because the spring mvc official has not made it clear that multiple instances are not allowed.
The reason I don't need it here is that it depends on how developers use it. If you define a lot of attributes in controller, then there will certainly be competitive access for singletons.
Therefore, as long as there are no attributes defined in the controller, the singleton is completely safe. Here is an example to illustrate:
Package com.lavasoft.demo.web.controller.lsh.ch6;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by Administrator on 14-4-9. * * @ author leizhimin 14-4-9 10:55 * / @ Controller@RequestMapping ("/ demo/lsh/ch6") @ Scope ("prototype") public class MultViewController {private static int st = 0; / static private int index = 0; / / non-static @ RequestMapping ("/ show") public String toShow (ModelMap model) {User user = new User (); user.setUserName ("testuname") User.setAge ("23"); model.put ("user", user); return "/ lsh/ch6/show";} @ RequestMapping ("/ test") public String test () {System.out.println (st++ + "|" + index++); return "/ lsh/ch6/test";}} will it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.