I am new to spring. On reading the documentation I found it's mentioned that
Bean default scope is a singleton.
If it has only a single instance in the container then "How it behaves in the multi-threaded environment ".
Typically in any web application, multiple requests will be done at the same time and if a POJO class is autowired and multiple requests are done at the same time then getter and setter will be crossing data of each other. Request 1 sets the data and same time request 2 override. Request 1 gets overridden data of request 2.
Like Foo is a POJO class whose get and set is not synchronized.
@Service
public class Foo {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Autowired
private Foo foo;
@RequestMapping(value = "foo" , method = RequestMethod.POST )
public String saveFoo(@RequestBody String fooname){
foo.setName(fooname); // In case of multiple request
//get and set will be happing on same instance could be wrong
return foo.getName();
}
What is the ideal way to handle Autowire and POJO in a multi-threaded environment? Does the developer had to mention the prototype scope ?