当前位置: 移动技术网 > IT编程>开发语言>Java > Spring类型转换 ConversionSerivce Convertor解析

Spring类型转换 ConversionSerivce Convertor解析

2019年07月22日  | 移动技术网IT编程  | 我要评论

以string转date为例:

定义转换器:

import java.text.parseexception;
import java.util.date;
import org.apache.commons.lang.time.dateutils;
import org.springframework.core.convert.converter.converter;
public class string2dateconverter implements converter<string, date> {
private string format = "yyyy-mm-dd";
public void setformat(string format){
this.format = format;
}
@override
public date convert(string arg0) {
try {
return dateutils.parsedate(arg0, new string[] { format });
} catch (parseexception e) {
return null;
}
}
}

配置spring:

<bean id="conversionservice" class="org.springframework.context.support.conversionservicefactorybean">
<property name="converters">
<list>
<bean class="com.xxx.string2dateconverter"/>
</list>
</property>
</bean>

以上所述是小编给大家介绍的spring类型转换 conversionserivce convertor,希望对大家有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网