Reputation: 23
I am having trouble finding a solid answer to this. I know that the controller is scoped singleton and instantiated once, same would be for any member variables in the controller class.
My question: is the incoming request object a new instance on each request? Assuming Jackson is mapping a JSON request body to the request object.
For example:
@RequestMapping(path = "/dosomething", method = POST)
@ResponseBody
public SomeObject doSomething(@RequestBody SomeObject someObject) {
// code here
return someObject;
}
I would like to process/store or otherwise change the state of someObject, but am not sure how safe that is when handling many requests, or if I need to explicitly instantiate a new SomeObject deep copied from the inbound request object.
Thanks for any help!
Upvotes: 2
Views: 1958
Reputation: 800
Yes. Parameters passed into a Spring controller are new instances each invocation.
Similar to any other singleton class, the instance of the singleton object is reused, but not parameters passed into the methods (unless of course you were reusing that object when you called the singleton more than once). But in a controller's case, the parameters are created from fresh HttpServletRequests each time, so there is no basis for reusing instances.
Upvotes: 2