自定義類型轉換器
201603005,今天想了一個問題,Spring中的Conventer是如何實現的,因為他沒有綁定類中的屬性,它怎么知道要將那個String轉換?看了幾遍的書也沒有找到,后來想想,其實我已經知道了,他的名字就是答案,轉換器,因為已經告訴他了目標類型,所以只要實體類中有這個類型,在使用springMVC表單對象時就會啟動吧(這個結論是我猜的),但是另外一個問題產生了,如果項目中有多個地方那個有了時間,一個時間格式是yyyy-MM-dd,另一個是yyyy-MM-dd HH:mm:ss,那么此時該如果讓Spring確定該用那個conventer呢(這個需求在我現在做的項目中就有這樣)??
1.conversionService是Spring類型轉換體系的核心借口
2.可以利用ConversionserviceFactoryBean在Spring的
IOC容器中定義一個ConversionService.
Spring將自動識別出IOC容器中的ConversionService,並在Bean
屬性配置及SpringMVC處理方法入參綁定場合使用它進行數據的轉換
3.可以通過ConversionServiceFactoryBean的ConversionServiceFactoryBean
的converters屬性注冊自定義的類型轉換器
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="app07a.formatter.DateFormatter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy" />
</bean>
</set>
</property>
</bean>
在上一篇文章中也有這個conversionService,但是沒有自定義錯誤消息,頁面直接把錯誤代碼直接顯示在頁面了.
在Spring中很多地方都用FactoryBean來整合第三方的框架的bean
Spring支持的轉換器
Spring定義了3種類型的轉換器借口,實現任意一個轉換器接口都可以作為自定義轉換器
注冊到ConversionServiceFactoryBean
-Converter<S,T>:將s類型轉換為T類型對象
-coverterFactory: 將相同系列多個"同質"converter封裝在一起,如果希望將一種
類型的對象轉換為另一種類型及其子類的對象(例如將String轉換為Number以及Number
子類(Integer,Long,Double等)對象)可使用該轉換器工廠類
-GenericConverter:會根據源類對象及目標對象所在的宿主類中的上下問信息進行類型轉換
<mvc:annotation-driver conversionService = "conversionService"/>會將自定義的ConversionService注冊到Spring MVC的上下文中
這個是一個簡單的轉換器
1頁面
<form action="http://localhost:8080/springMVCapp07a/testConversionServiceConverter" method="post"> <!-- private String name; private String description; private Float price; private Date productionDate; --> <!--格式:java-javabase-12.0-2011.12.12 --> product:<input type="text" name="product"> <input type="submit" value="submit"> </form>
轉換器的類
@Component public class ProcuctConverter implements Converter<String, Product> { @Override public Product convert(String source) { if(source != null) { Product product = new Product(); //java-javabase-12.0-2011.12.12 String[] values = source.split("-"); if(values != null && values.length==4) { product.setName(values[0]); product.setDescription(values[1]); product.setPrice(Float.parseFloat(values[2])); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); product.setProductionDate(new Date()); System.out.println(source + "--converter"+product); return product; } } return null; }
springmvc配置文件
<!--配置ConversionService --> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="procuctConverter"/> </set> </property> </bean>
記得要在annotation上加上這句
<mvc:annotation-driven conversion-service="conversionService" />
目標action
@Controller public class testConversionService { @RequestMapping(value="/testConversionServiceConverter") public String test(@RequestParam("product") Product product ) { System.out.println("/testConversionServiceConverter"+product); return "ProductForm"; } }
現在再按照上篇博客的做法來找找咱們自己寫的這個converter
ConversionService converters =
java.lang.Boolean -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@5844d84b
java.lang.Character -> java.lang.Number : org.springframework.core.convert.support.CharacterToNumberFactory@69367ae0
java.lang.Character -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@7857fecb
java.lang.Enum -> java.lang.String : org.springframework.core.convert.support.EnumToStringConverter@29f35b69
java.lang.Number -> java.lang.Character : org.springframework.core.convert.support.NumberToCharacterConverter@2a3022e3
java.lang.Number -> java.lang.Number : org.springframework.core.convert.support.NumberToNumberConverterFactory@3b1e1d56
java.lang.Number -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@7847604d
java.lang.String -> app07a.domain.Product : app07a.converters.ProcuctConverter@327961d9
java.lang.String -> java.lang.Boolean : org.springframework.core.convert.support.StringToBooleanConverter@1f22dcc2
java.lang.String -> java.lang.Character : org.springframework.core.convert.support.StringToCharacterConverter@3368b15d
java.lang.String -> java.lang.Enum : org.springframework.core.convert.support.StringToEnumConverterFactory@7348de9a
java.lang.String -> java.lang.Number : org.springframework.core.convert.support.StringToNumberConverterFactory@68874644
java.lang.String -> java.util.Locale : org.springframework.core.convert.support.StringToLocaleConverter@203e4659
java.lang.String -> java.util.Properties : org.springframework.core.convert.support.StringToPropertiesConverter@6b4fedc9
java.lang.String -> java.util.UUID : org.springframework.core.convert.support.StringToUUIDConverter@a0a5207
java.time.ZoneId -> java.util.TimeZone : org.springframework.core.convert.support.ZoneIdToTimeZoneConverter@3e4c9e1d
java.time.ZonedDateTime -> java.util.Calendar : org.springframework.core.convert.support.ZonedDateTimeToCalendarConverter@346cc261
java.util.Locale -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@5e465495
java.util.Properties -> java.lang.String : org.springframework.core.convert.support.PropertiesToStringConverter@5854cbda
java.util.UUID -> java.lang.String : org.springframework.core.convert.support.ObjectToStringConverter@66845d2f
org.springframework.core.convert.support.ArrayToArrayConverter@54072bf1
org.springframework.core.convert.support.ArrayToCollectionConverter@7231c593
org.springframework.core.convert.support.ArrayToObjectConverter@404beb79
org.springframework.core.convert.support.ArrayToStringConverter@4ef8c947
org.springframework.core.convert.support.ByteBufferConverter@5208841f
org.springframework.core.convert.support.ByteBufferConverter@5208841f
org.springframework.core.convert.support.CollectionToArrayConverter@c45da14
org.springframework.core.convert.support.CollectionToCollectionConverter@10ae797b
org.springframework.core.convert.support.CollectionToObjectConverter@8a04154
org.springframework.core.convert.support.CollectionToStringConverter@5df613f4
org.springframework.core.convert.support.FallbackObjectToStringConverter@25a8080b
org.springframework.core.convert.support.IdToEntityConverter@69f094c4,org.springframework.core.convert.support.ObjectToObjectConverter@7fcecd99
org.springframework.core.convert.support.MapToMapConverter@6c4f3ba0
org.springframework.core.convert.support.ObjectToArrayConverter@554c4aeb
org.springframework.core.convert.support.ObjectToCollectionConverter@3f0c59b9
org.springframework.core.convert.support.StringToArrayConverter@234d2bca
org.springframework.core.convert.support.StringToCollectionConverter@17af60fb
Spring自帶的轉換器都在,紅色標記的就是自己寫的