我有使用SpringMVC的以下Controller代码:
@Controller
@Scope("prototype")
@RequestMapping("/messages")
public class MessageController {
@RequestMapping(value="/index",method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public String displayAllMessages(ModelMap model) {
System.out.println(this.hashCode());
// processing
return "messages";
}
}
当使用@Scope(“prototype”)时,每个请求都会出现,this.hashCode()的输出是不同的,这意味着当每个请求到来时,将创建一个新的MessageController实例.
如果不使用@Scope(“prototype”),默认将是@Scope(“singleton”),每个请求都来了,this.hashCode()的输出是相同的,这意味着只创建了一个MessageController实例.
我不确定什么时候应该使用@Scope(“原型”),什么时候不能?