I am experiencing some confusion in the use and purpose of Spring's DataBinder and ConversionService with regards to binding web requests to model objects. This has arisen because I have recently tried to use the JSR-303 validation by adding .

Prior to this I used:

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="mypackage.GlobalWebBindingInitializer" />
    </property>
</bean>

This was good because I wanted a global DataBinder that could be used by several Controllers. Within the GlobalWebBindingInitialzer class implement several of these:

binder.registerCustomEditor(MyClass.class, new PropertyEditorSupport(MyClass.class)

However I wanted to use the @Valid annotation and so added . The side-effect of this is that the above AnnotationMethodHandlerAdapter bean is already defined as part of the annotation-driven and so my global data binder is ignored.

So now I have created this class:

public class MyClassConverter implements Converter<String, MyClass>

I am confused. If I want to use should I use conversion service rather than databinder?

有帮助吗?

解决方案

Historically Spring's data binding was used to convert data into javabeans. It relies heavily on JavaBean PropertyEditors to do the conversion.

Spring 3.0 added new and different support for conversions and formatting. Some of the changes included a "core.convert" package and a "format" package that as per the docs "may be used as simpler alternatives to PropertyEditors."

Now, to answer your question, yes, it looks like you're on the right track. You can continue to use either, but to make a long story short in many cases you should be able to use a converter instead of a data binder.

Documentation about how to add validation is available online.

其他提示

Further to answer above PropertyEditors (esp PropertyEditorSupport) are not thread safe which is especially required in a Web environment where each request is served in a separate thread. Theoretically, PropertyEditors should yield unpredictable results under highly concurrent conditions.

But not sure why Spring used PropertyEditors in the first place. May be it was meant for non-multithread environments and dates before SpringMVC?

EDIT:

Although, PropertyEditorSupport doesn't look thread safe Spring ensures its used in a thread safe manner. For example, initBinder() is called every time data binding is needed. I was wrong on the notion that its called only once when controller is initialized.

@InitBinder
public void initBinder(WebDataBinder binder) {

    logger.info("initBinder() called.");

    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    dateFormat.setLenient(false);

    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

Here the log "initBinder() called." can show up multiple times whenever binding occurs.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top