Spring类型转换 ConversionSerivce Convertor解析
以String转Date为例:
定义转换器:
importjava.text.ParseException; importjava.util.Date; importorg.apache.commons.lang.time.DateUtils; importorg.springframework.core.convert.converter.Converter; publicclassString2DateConverterimplementsConverter<String,Date>{ privateStringformat="yyyy-MM-dd"; publicvoidsetFormat(Stringformat){ this.format=format; } @Override publicDateconvert(Stringarg0){ try{ returnDateUtils.parseDate(arg0,newString[]{format}); }catch(ParseExceptione){ returnnull; } } }
配置Spring:
<beanid="conversionService"class="org.springframework.context.support.ConversionServiceFactoryBean"> <propertyname="converters"> <list> <beanclass="com.xxx.String2DateConverter"/> </list> </property> </bean>
以上所述是小编给大家介绍的Spring类型转换ConversionSerivceConvertor,希望对大家有所帮助。