- 我们先看一种最普通的情况 model和Controller都是单例
@Component
public class Order {
private String orderNum = "12";
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
@Override
public String toString() {
return "Order{" +
"orderNum='" + orderNum + '\'' +
'}';
}
}
@Controller
@RequestMapping(value = "/api")
public class MainController {
@Autowired
private Order order;
private String name;
@RequestMapping(value = "/users/{username}",method = RequestMethod.GET)
@ResponseBody
public String userProfile(@PathVariable("username") String username) {
name = username;
order.setOrderNum( name);
try {
for(int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getId() + "name:" + name + "--order:" + order.getOrderNum());
Thread.sleep(2000);
}
} catch (Exception e) {
}
return order.toString();
}
}
我们分别发送两个请求
http://localhost:8080/api/users/aaa
http://localhost:8080/api/users/bbb
29name:aaa--order:aaa
29name:aaa--order:aaa
29name:aaa--order:aaa
31name:bbb--order:bbb
29name:bbb--order:bbb
31name:bbb--order:bbb
可以看到当第二个请求发送后,29线程里的name和order都发生了改变,因为当前的Order和Controller都是单例
- 情况二 model是单例 Controller不是单例
@Component
public class Order {
private String orderNum = "12";
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
@Override
public String toString() {
return "Order{" +
"orderNum='" + orderNum + '\'' +
'}';
}
}
@Controller
@RequestMapping(value = "/api")
@Scope("prototype")
public class MainController {
@Autowired
private Order order;
private String name;
@RequestMapping(value = "/users/{username}",method = RequestMethod.GET)
@ResponseBody
public String userProfile(@PathVariable("username") String username) {
name = username;
order.setOrderNum( name);
try {
for(int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getId() + "name:" + name + "--order:" + order.getOrderNum());
Thread.sleep(2000);
}
} catch (Exception e) {
}
return order.toString();
}
}
28name:aaa--order:aaa
28name:aaa--order:aaa
29name:bbb--order:bbb
28name:aaa--order:bbb
29name:bbb--order:bbb
28name:aaa--order:bbb
可以看到第二个请求发送后,28线程中的name还是aaa没有发生变化,而order受到了影响,发生了变化,因为Controller是一个新的,所以name也是一个新的变量,但是order是单例的
3.情况三 model不是单例 Controller是单例
@Component
@Scope("prototype")
public class Order {
private String orderNum = "12";
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
@Override
public String toString() {
return "Order{" +
"orderNum='" + orderNum + '\'' +
'}';
}
}
@Controller
@RequestMapping(value = "/api")
public class MainController {
@Autowired
private Order order;
private String name;
@RequestMapping(value = "/users/{username}",method = RequestMethod.GET)
@ResponseBody
public String userProfile(@PathVariable("username") String username) {
name = username;
order.setOrderNum( name);
try {
for(int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getId() + "name:" + name + "--order:" + order.getOrderNum());
Thread.sleep(2000);
}
} catch (Exception e) {
}
return order.toString();
}
}
29name:aaa--order:aaa
29name:aaa--order:aaa
30name:bbb--order:bbb
29name:bbb--order:bbb
30name:bbb--order:bbb
可以看到name和order都变了,可见只设置Model是没有用的
4.情况四 Controller和Model都设置不是单例
@Component
@Scope("prototype")
public class Order {
private String orderNum = "12";
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
@Override
public String toString() {
return "Order{" +
"orderNum='" + orderNum + '\'' +
'}';
}
}
@Controller
@RequestMapping(value = "/api")
@Scope("prototype")
public class MainController {
@Autowired
private Order order;
private String name;
@RequestMapping(value = "/users/{username}",method = RequestMethod.GET)
@ResponseBody
public String userProfile(@PathVariable("username") String username) {
name = username;
order.setOrderNum( name);
try {
for(int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getId() + "name:" + name + "--order:" + order.getOrderNum());
Thread.sleep(2000);
}
} catch (Exception e) {
}
return order.toString();
}
}
28name:aaa--order:aaa
28name:aaa--order:aaa
29name:bbb--order:bbb
28name:aaa--order:aaa
29name:bbb--order:bbb
28name:aaa--order:aaa
这样29请求的和28请求的就完全分开了
网友评论