Spring中DataBinder和ConversionService的区别
在将 Web 请求绑定到模型对象方面,我对 Spring 的 DataBinder 和 ConversionService 的使用和目的感到有些困惑。出现这种情况是因为我最近尝试通过添加 .
在此之前我使用:
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="mypackage.GlobalWebBindingInitializer" />
</property>
</bean>
这很好,因为我想要一个可以由多个控制器使用的全局 DataBinder。 在 GlobalWebBindingInitialzer 类中实现其中几个:
binder.registerCustomEditor(MyClass.class, new PropertyEditorSupport(MyClass.class)
但是我想使用 @Valid 注释,因此添加了 .这样做的副作用是上面的 AnnotationMethodHandlerAdapter bean 已经定义为注释驱动的一部分,因此我的全局数据绑定器被忽略。
所以现在我创建了这个类:
public class MyClassConverter implements Converter<String, MyClass>
我很困惑。如果我想使用我应该使用转换服务而不是databinder吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
历史上,Spring 的数据绑定用于将数据转换为 javabean。它严重依赖 JavaBean PropertyEditors 来完成转换。
Spring 3.0 添加了新的和不同的支持格式化。一些更改包括“core.convert”包和“format”包,根据文档“可以用作 PropertyEditors 的更简单替代品”。
现在,回答你的问题,是的,看起来你走在正确的轨道上。您可以继续使用其中任何一个,但长话短说,在许多情况下您应该能够使用转换器而不是数据绑定器。
有关如何添加验证 的文档可在线获取。
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.
进一步回答上面的 PropertyEditors (esp PropertyEditorSupport) 不是线程安全的,这在每个请求都在单独的线程中提供服务的 Web 环境中尤其需要。理论上,PropertyEditors 在高度并发的条件下应该会产生不可预测的结果。
但不知道为什么 Spring 首先使用 PropertyEditors。也许它是针对非多线程环境和 SpringMVC 之前的日期的?
编辑:
虽然,PropertyEditorSupport看起来不是线程安全的,但Spring确保它以线程安全的方式使用。例如,每次需要数据绑定时都会调用 initBinder()。我错误地认为它在控制器初始化时只调用一次。
这里是日志“调用了 initBinder()”。每当绑定发生时都可以多次显示。
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.
Here the log "initBinder() called." can show up multiple times whenever binding occurs.