Let's say you pig-like code, and do something like this in your controller:
private List<String> allMessages; public String displayAllMessages(ModelMap model) { allMessages = new ArrayList<>(); fillMessages(); model.put(messages, allMessages); return "messages"; } private void fillMessages() { allMessages.add("hello world"); }
The controller will work with state: it has a state ( allMessages ), which cannot be shared between two requests. The controller is no longer thread safe. If it was called simultaneously to handle two simultaneous requests, there may be a race condition.
You can avoid this problem by making the controller a prototype: each request will be processed by a separate controller.
Or you could do the right thing and make the code standstill, and in this case, creating a new controller for each request would be useless, as the controller would be stateless and therefore thread safe. Then the scope can save the default value: singleton.
public String displayAllMessages(ModelMap model) { List<String> messages = fillMessages(); model.put(messages, allMessages); return "messages"; } private List<String> fillMessages() { List<String> allMessages = new ArrayList<>(); allMessages.add("hello world"); return allMessages; }
source share