java - Spring 中的 PropertyEditor、Formatter 和 Converter 有什么区别?

标签 java spring spring-boot spring-mvc

使用 Spring MVC事实证明,我可以用许多不同的方式绑定(bind)我的表格,我真的觉得自己迷路了。 parseprint Formatter 的方法相当于那些 PropertyEditorSupport使用不同的名称( getAsTextsetAsText )。同样,我可以实现 GenericConverter 或两个 Converter<S,T>让我们做同样的事情。

我读了here在评论中 FormattersPropertyEditor 的替代品,但我还没有找到任何支持它的文档,它甚至都没有被弃用。

我的问题是,当涉及到将数据从表单绑定(bind)到对象时,在 spring-mvc 中执行此操作的正确方法是什么? ? PropertyEditor 之间的主要区别是什么? , FormatterConverter在 Spring ?每个的用例是什么?对我来说,他们似乎负有同样的责任。

最佳答案

为了帮助理解这些概念,我首先要区分 Spring 的特定功能与 Java 公开的功能。

PropertyEditorthe related stuffJavaBeans Specification 定义.

该规范定义了一个 API、机制和约定,用于处理对象、对象属性以及与其更改相关的所有内容,如事件。

PropertyEditor 通常在 GUI 中用于处理 UI 和底层对象模型之间的交互,通常处理属性值与其 String 表示之间的转换.

Spring 本身实际上在许多不同的情况下使用不同的 PropertyEditor 实现和 Java Beans 约定。例如,来自 docs :

A couple of examples where property editing is used in Spring:

  • Setting properties on beans is done by using PropertyEditor implementations. When you use String as the value of a property of some bean that you declare in an XML file, Spring (if the setter of the corresponding property has a Class parameter) uses ClassEditor to try to resolve the parameter to a Class object.

  • Parsing HTTP request parameters in Spring’s MVC framework is done by using all kinds of PropertyEditor implementations that you can manually bind in all subclasses of the CommandController.

总而言之,PropertyEditor 允许您使用更多的案例。

现在,在 Spring 世界中,您还需要区分 Spring MVC 和 Spring Core。

请注意 ConvertFormatter东西被定义为核心技术,与任何用例相关,不限于网络框架。

Spring 文档,在描述时 Spring Field Formatting , 提供了关于每个 API/SPI 的目的以及它们如何与 PropertyEditor 相关的很好的解释:

As discussed in the previous section, core.convert is a general-purpose type conversion system. It provides a unified ConversionService API as well as a strongly typed Converter SPI for implementing conversion logic from one type to another. A Spring container uses this system to bind bean property values. In addition, both the Spring Expression Language (SpEL) and DataBinder use this system to bind field values. For example, when SpEL needs to coerce a Short to a Long to complete an expression.setValue(Object bean, Object value) attempt, the core.convert system performs the coercion.

Now consider the type conversion requirements of a typical client environment, such as a web or desktop application. In such environments, you typically convert from String to support the client postback process, as well as back to String to support the view rendering process. In addition, you often need to localize String values. The more general core.convert Converter SPI does not address such formatting requirements directly. To directly address them, Spring 3 introduced a convenient Formatter SPI that provides a simple and robust alternative to PropertyEditor implementations for client environments.

In general, you can use the Converter SPI when you need to implement general-purpose type conversion logic — for example, for converting between a java.util.Date and a Long. You can use the Formatter SPI when you work in a client environment (such as a web application) and need to parse and print localized field values. The ConversionService provides a unified type conversion API for both SPIs.

在 Spring MVC 的特定用例中,框架本身能够在 handling the HTTP requests 时处理简单类型.

Type conversion根据配置的转换器集自动应用,尽管可以使用 DataBinders 调整该行为和前面提到的格式化系统。请参阅 relevant docs .

在处理读取和写入 HTTP 请求和响应正文的典型用例中,当使用 @RequestBody 时,例如,Spring 会使用一堆不同的预 configured HttpMessageConverter实现:实际注册的将取决于您的配置和项目中导入的库 - 例如 Jackson。我无法在文档中找到这一点,但这里是实际 source code 的链接.

请考虑审查 this related SO question , 它可能会有所帮助。

关于java - Spring 中的 PropertyEditor、Formatter 和 Converter 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71515687/

相关文章:

spring - 返回 JSON 作为 Spring Boot 响应

java - 如何从sqlite数据库android获取一个值?

java - 递归地从矩阵中检索权重最低的路径

java - 在 Spring 的 Controller 中存储状态

java - 连接池错误(Spring, hibernate )。如何解决这个问题?

java - 如何在 Spring Boot 中安排延迟增加的任务?

java - org.springframework.beans.factory.BeanCreationException : Error How do I solve this?

java - 配置Spring JMS监听器时如何设置WMQ_MQMD_READ_ENABLED

java - 区分是否是重定向: or a normal request in HandlerInterceptorAdaptor

spring - org.springframework.beans.factory.BeanCreationException 创建 ServletContext 中定义的名称为 'sessionFactory' 的 bean 时出错