Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Spring data binding work on the background?

I'm beginning to understand a little bit of what happens on the background when a user requests some URL in the context of a Spring MVC Web Application. But I can't figure out when and how takes place the data binding, i.e. the mapping between the fields of an input form to a model object and the later injection of that object in the related handler method.

Anybody knows which classes and methods are implied, and where does this appear in the documentation?

like image 405
Rocío García Luque Avatar asked Apr 05 '14 16:04

Rocío García Luque


1 Answers

If, for example, you're posting data which represents SomeBean to this handler: public void (SomeBean someBean) the following happens in Spring 4.

  • A InvocableHandlerMethod will iterate over the method's parameters and will ask a HandlerMethodArgumentResolverComposite instance to resolve the value for each parameter.

  • The HandlerMethodArgumentResolverComposite will ask it's HandlerMethodArgumentResolvers if they support the given parameter. If one supports the parameter this one is used for resolving the value.

  • A ModelAttributeMethodProcessor will support the given parameter (SomeBean) and will try to resolve it's value.

  • Inside it's resolveArgument method the ModelAttributeMethodProcessor will instantiate a target object of parameter's type.

  • It then will call a WebDataBinderFactory to create a WebDataBinder which is responsible for binding the request to the target object.

  • Once binded, the target will be returned and provided as an argument for the handler method.

P.s. Instead of the documentation you should read the source code.

like image 58
Bart Avatar answered Oct 05 '22 01:10

Bart