springMVC源码4:HttpMessageConverter和ConversionService之间怎么关联(上)

时间:2022-09-01 20:41:42

1 spring数据绑定:http://jinnianshilongnian.iteye.com/blog/1723270

2 springMVC中,HttpMessageConverter<T>和ConversionService之间的区别:http://www.iteye.com/problems/98525

   ConversionService是属于spring core部分 所有spring管理的bean等等 都使用它进行类型转换,而它又需要注册一些converter 来完成类型转换。 这个可以看我博客 springmvc部分 
HttpMessageConverter 是对http请求/响应 数据进行转换的, 它可以使用ConversionService进行一些转换(HttpMessageConverter是否能转换 还需要根据如请求的contentType等决定).

意思是:HttpMessageConverter 使用ConversionService进行一些转换。

3 官方文档:

    In a Spring MVC application, you may configure a custom ConversionService instance explicitly as an attribute of the annotation-driven element of 
the MVC namespace. This ConversionService will then be used anytime a type conversion is required during Controller model binding. If not configured 
explicitly, Spring MVC will automatically register default formatters and converters for common types such as numbers and dates.
翻译:
    在Spring MVC应用程序中,您可以将自定义ConversionService实例显式配置为MVC命名空间的注释驱动元素的属性。 随后在Controller模型绑定期间需要进行类型转换时,将使用此ConversionService。 如果没有明确配置,Spring MVC将自动注册默认格式化程序和转换器,用于常见类型,如数字和日期。

<mvc:annotation-driven/>
    With this one-line of configuration, default formatters for Numbers and Date types will be installed, including support for the @NumberFormat and
 @DateTimeFormat annotations. Full support for the Joda Time formatting library is also installed if Joda Time is present on the classpath.
To inject a ConversionService instance with custom formatters and converters registered, set the conversion-service attribute and then 
specify custom converters, formatters, or FormatterRegistrars as properties of the FormattingConversionServiceFactoryBean:

通过这种一行配置,将使用默认“数字和日期”的格式化方法,包括对@NumberFormat和@DateTimeFormat注释的支持。 如果Joda时间存在于类路径上,
则还将完全支持Joda Time格式化库。要注册自定义格式化程序和转换器的ConversionService实例,请设置conversion-service属性,然后将使用自定义converters,

formatters或FormatterRegistrars,作为FormattingConversionServiceFactoryBean的三个属性:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven conversion-service="conversionService"/>

    <bean id="conversionService"
          class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="org.example.MyConverter"/>
            </set>
        </property>
        <property name="formatters">
            <set>
                <bean class="org.example.MyFormatter"/>
                <bean class="org.example.MyAnnotationFormatterFactory"/>
            </set>
        </property>
        <property name="formatterRegistrars">
            <set>
                <bean class="org.example.MyFormatterRegistrar"/>
            </set>
        </property>
    </bean>

</beans>

总结:
7.6.5 Configuring Formatting in Spring MVC
In a Spring MVC application, you may configure a custom ConversionService instance explicitly as an attribute of the annotation-driven element of the MVC namespace. This ConversionService will then be used anytime a type conversion is required during Controller model binding. If not configured explicitly, Spring MVC will automatically register default formatters and converters for common types such as numbers and dates.
在Spring MVC应用程序中,您可以将自定义ConversionService实例显式配置为MVC命名空间的注释驱动元素的属性。 随后在Controller模型绑定期间需要进行类型转换时,将使用此ConversionService。 如果没有明确配置,Spring MVC将自动注册默认格式化程序和转换器,用于常见类型,如数字和日期。

关键在这一句话: ConversionService实例显式配置为MVC命名空间的注释驱动元素的属性则使用ConversionService。
                              如果没有明确配置,Spring MVC将自动注册默认格式化程序和转换器,用于常见类型,如数字和日期。
Spring3引入了更加通用的类型转换系统,其定义了SPI接口(Converter等)和相应的运行时执行类型转换的API(ConversionService等):见下篇http://blog.csdn.net/wabiaozia/article/details/72869098
(HttpMessageConverter里默认的几个支持,见http://blog.csdn.net/wabiaozia/article/details/50803581/ 几种默认的HttpMessageConverter 支持。当然可以自定义HttpMessageConverter里的具体转换类),也可以用ConversionService实例显示配置代替原有的默认ConversionService。
###虽然写了那么多,估计你还是想问,那么他们到底哪里有关联??????????????
还是这句话,HttpMessageConverter 是对http请求/响应 数据进行转换的, 它可以使用ConversionService进行一些转换。
ObjectToStringHttpMessageConverter是HttpMessageConverter 提供的,但是ObjectToStringHttpMessageConverter却调用了conversionService去转换。
ObjectToStringHttpMessageConverter(我看的是spring-web-3.2.6.RELEASE.jar和4.3.5版本)里源码:使用了conversionService去转换。
关于ObjectToStringHttpMessageConverter的介绍:An {@code HttpMessageConverter} that uses {@link StringHttpMessageConverter}
 for reading and writing content and a  ConversionService  for converting the String content to and from the target object type.
By default, this converter supports the media type {@code text/plain} only. This can be overridden through the {@link #setSupportedMediaTypes supportedMediaTypes} property.