官方文档 在转换类中继承PropertyEditorSupport 并从写setAsText方法 将处理好的值传入 setValue();
bean方法中固定格式修改map中参数 使用键值对声明配置需要转换的类型 ,转换方法的类路径
public class 转换类 extends PropertyEditorSupport {
public void setAsText(String text) {
//处理代码段
setValue(new ExoticType(text.toUpperCase()));
}
}
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
// 转换的类型 转换的类路径
<entry key="example.ExoticType" value="example.ExoticTypeEditor"/>
</map>
</property>
</bean>
{
@我们来举个栗子 把String转换成date
public class String2Date extends PropertyEditorSupport
public void setAsText(String text) throws IllegalArgumentException {
Date date=null;
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
try {
date = sdf.parse(text);
} catch (java.text.ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
setValue(date);
}
<!--------- 配置属性转换器 修改map标签内的内容 键值对方式key:遇到生么样的类型---------- -->
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util.Date" value="comment.String2Date"/>
</map>
</property>
</bean>
<bean id="stu" class="emtity.Student">
<property name="id" value="15"></property>
<property name="name" value="小明"></property>
<property name="age" value="19"></property>
<property name="brithday" value="1999-12-12"></property>
</bean>
}