所有请求均由控制器的一个实例处理(单例,因为它是一个Spring托管bean)。因此,您需要确保不存储与一个请求相关的任何状态(在字段中)。
所以:
@Controller@RequestMapping("/foo")public class Foo { @Autowired private Something something; @RequestMapping("/list") public String foo() { something.someMethod(); bar(); return "view" } private void bar() { // something }}可以,但是:
@Controller@RequestMapping("/foo")public class Foo { private User theUser; // problem is ALL request share this field @RequestMapping("/foo/{userId}") public String foo(@PathVariable final Integer userId) { if (theUser.getId().equals(userId)) {// something } else {theUser = ... } return "view" }}不是。
注意:未经测试(在此处键入内容可能会伤害您的编译器)



