We are working on a Spring 3.0.5 Web MVC-based application. In our code we quite often do something like this:
@ModelAttribute(ModelKeys.SOME_BEAN)
public SomeBean newSomeBean() {
return new SomeBean();
}
I think this is not necessary. But if it really wasn't, then I wonder how has this managed to slip through so many code reviews? In my understanding, if a controller method wanted a NEW SomeBean, then annotating a parameter in that method with @ModelAttribute should be enough? Spring would then use the default constructor to new up the required bean for invoking the controller method, e.g.:
@RequestMapping(method = RequestMethod.POST)
public String doIt(
@ModelAttribute(ModelKeys.SOME_BEAN) final SomeBean bean,
final BindingResult bindingResult)
{
...
}
here, Spring would new up an instance of SomeBean and try to data-bind into it from the POSTed data, right? There's no need for the method as shown in the first code snippet? Please can you confirm this or provide me with your thoughts on this? Would I be introducing a risk if I just went ahead and removed all these methods that do nothing other than new up an empty bean?