In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 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: 286
*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.